怪文書 / Dubious Document(AtCoder-2393)
Problem Description
Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.
He will receive a headline which contains one of the strings S1,…,Sn tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.
Find the longest string that can be created regardless of which string among S1,…,Sn the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.
Constraints
- 1≤n≤50
- 1≤|Si|≤50 for every i=1,…,n.
- Si consists of lowercase English letters (
a-z) for every i=1,…,n.Input
Input is given from Standard Input in the following format:
n
S1
…
SnOutput
Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.
Example
Sample Input 1
3
cbaa
daacc
acacacSample Output 1
aac
The strings that can be created from each of cbaa, daacc and acacac, are aa, aac, aca, caa and so forth. Among them, aac, aca and caa are the longest, and the lexicographically smallest of these three is aac.Sample Input 2
3
a
aa
bSample Output 2
The answer is an empty string.
题意:n 个字符串,找出每个字符串中共有的字符,统计他们的个数,挑选个数最小的,最后将挑选出来的字符按字典序排列
思路:建一个二维的桶,将第 i 个字符串中的字符存进桶中,然后取第 i 个桶的每个字符的最小值,最后根据最小值顺序输出即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
char str[N][N];
int bucket[N][27];
int vis[27];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s",str[i]+1);
        int len=strlen(str[i]+1);
        for(int j=1;j<=len;j++){
            int temp=str[i][j]-'a'+1;
            bucket[i][temp]++;
        }
    }
    for(int i=1;i<=26;i++)
        vis[i]=bucket[1][i];
    for(int i=1;i<=n;i++){
        for(int j=1;j<=26;j++){
            vis[j]=min(vis[j],bucket[i][j]);
        }
    }
    
    for(int i=1;i<=26;i++){
        while(vis[i]--){
            char temp=(char)(i+'a'-1);
            printf("%c",temp);
        }
    }
    
    return 0;
}
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号