天天看点

(字符串连接)2204 字符串连接2204 字符串连接输入输出输入样例输出样例1097 拼成最小的数输入输出输入样例输出样例

2204 字符串连接

  1. 1 秒
  2. 131,072 KB
  3. 20 分
  4. 3 级题

输入n个字符串s[i],你要把他们按某个顺序连接起来,使得字典序最小。

(1 <= n <= 100)

(每个字符串长度 <= 100)

(字符串只包含小写字母)

 收起

输入

第一行一个整数n。
接下来每行一个字符串s[i]。
           

输出

一行一个字符串表示把输入的n个字符串按某个顺序连接之后的结果
           

输入样例

6
it
looks
like
an
easy
problem
           

输出样例

aneasyitlikelooksproblem
           

题解:cba 

           cbaa

           ans  =  cbaacba

#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int M=1e3+5;
const int N=1e5+5;
struct fun{
    string str;
}f[150];

bool cmp(fun x,fun y){
    return (x.str+y.str)<(y.str+x.str);
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        cin>>f[i].str;
    sort(f,f+n,cmp);
    for(int i=0;i<n;i++)
        cout<<f[i].str;
    return 0;
}
           

1097 拼成最小的数

  1. 1 秒
  2. 131,072 KB
  3. 20 分
  4. 3 级题

设有n个正整数,将它们联接成一排,组成一个最小的多位整数。

例如:

n=2时,2个整数32,321连接成的最小整数为:32132,

n=4时,4个整数55,31,312, 33 联接成的最小整数为:312313355

 收起

输入

第1行:1个数N。(2 <= N <= 10000)
第2 - N + 1行:每行1个正整数。(1 <= A[i] <= 10^9)      

输出

输出拼在一起的最小整数。由于数据量太大,请以1000个字符为单位,输出到一行里,最终剩余的不足1000个字符的部分,输出到单独1行。      

输入样例

4
55
31
312
33      

输出样例

312313355      
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int M=1e3+5;
const int N=1e5+5;
struct fun{
    string str;
}f[N];

bool cmp(fun x,fun y){
    return (x.str+y.str)<(y.str+x.str);
}
int main(){
    int n;
    scanf("%d",&n);
    getchar();
    for(int i=0;i<n;i++)
        cin>>f[i].str;

    sort(f,f+n,cmp);
    string str="";
    for(int i=0;i<n;i++)
        str+=f[i].str;
    int len=str.length();
    for(int i=0;i<len;i++){
        printf("%c",str[i]);
        if((i+1)%1000==0)
            printf("\n");
    }
    return 0;
}