天天看點

(字元串連接配接)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;
}