AT2393 [ARC071A] 怪文書 / Dubious Document题解

· · 题解

Solution

题意:有 n 个字符串,求每个字母在字符串里出现的最小次数,最后按顺序依次输出这些字母出现的次数个字母。

对于每个字符串,我们依次读取出其出现字母,并将对应的桶内的数量进行修改。

        memset(a,0,sizeof(a));
        cin>>st;
        for(register int j=0; j<st.length(); ++j) {
            a[st[j]-'a']++;
        }

在讲这些次数依次和原有的进行比较,取其中较小的。(如果是第一次则直接赋值)

Code:

#include<bits/stdc++.h>
using namespace std;
inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)) {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(isdigit(ch)) {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
typedef long long ll;
int n,a[30],ans[30];
string st;
int main() {
    n=read();
    for(register int i=1; i<=n; ++i) {
        memset(a,0,sizeof(a));
        cin>>st;
        for(register int j=0; j<st.length(); ++j) {
            a[st[j]-'a']++;
        }
        if(i!=1) {
            for(register int j=0; j<26; ++j) {
                ans[j]=min(ans[j],a[j]);
            }
        } else {
            for(register int j=0; j<26; ++j) {
                ans[j]=a[j];
            }
        }
    }
    for(register int j=0; j<26; ++j) {
        while(ans[j]!=0) {
            printf("%c",j+'a');
            ans[j]--;
        }
    }printf("\n");
    return 0;
}