分类讨论——cf903E

模拟大讨论就完事了!

#include<bits/stdc++.h>
using namespace std;
#define N 5005

int n,K,cnt[26],mx,tmp[26];
char s[N][N],t1[N],t2[N];

int judge(char *s,char *t){
    int res=0;
    for(int i=1;i<=K;i++)
        if(s[i]!=t[i])res++;
    return res;
}

int main(){
    cin>>n>>K;
    for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
    for(int i=1;i<=K;i++)cnt[s[1][i]-'a']++;
    for(int i=0;i<26;i++)mx=max(mx,cnt[i]);
    
    for(int i=1;i<=n;i++){
        memset(tmp,0,sizeof tmp); 
        for(int j=1;j<=K;j++)tmp[s[i][j]-'a']++;
        for(int j=0;j<26;j++)
            if(tmp[j]!=cnt[j]){cout<<-1<<'
';return 0;}
    }
    
    int Max=0;
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
            Max=max(Max,judge(s[i],s[j]));

    if(Max==0){
        swap(s[1][1],s[1][2]);
        cout<<(s[1]+1)<<endl;
        return 0;
    }
    if(Max==1 || Max>=5){
        cout<<-1<<'
';
        return 0;
    }
    if(Max==2){
        if(mx>=2){
            cout<<(s[1]+1)<<'
';
            return 0;
        }
        cout<<-1<<'
';
        return 0;
    }
    if(Max==3){
        for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
            if(judge(s[i],s[j])==3){
                memcpy(t1,s[i],sizeof s[i]);
                memcpy(t2,s[j],sizeof s[j]);
                int pos[5],id=0;
                for(int k=1;k<=K;k++)
                    if(t1[k]!=t2[k])pos[++id]=k;
                
                int dif;
                dif=0;
                swap(t1[pos[1]],t1[pos[2]]);
                for(int l=1;l<=n;l++){
                    int tot=judge(s[l],t1);
                    if(tot==0 && mx<2){
                        cout<<-1;return 0;
                    }
                    dif=max(dif,tot);
                }
                if(dif==2){
                    cout<<(t1+1)<<'
';
                    return 0;
                }
                swap(t1[pos[1]],t1[pos[2]]);
                
                dif=0;
                swap(t1[pos[1]],t1[pos[3]]);
                for(int l=1;l<=n;l++){
                    int tot=judge(s[l],t1);
                    if(tot==0 && mx<2){
                        cout<<-1;return 0;
                    }
                    dif=max(dif,tot);
                }
                if(dif==2 || dif==0&&mx>=2){
                    cout<<(t1+1)<<'
';
                    return 0;
                }
                swap(t1[pos[1]],t1[pos[3]]);
                
                dif=0;
                swap(t1[pos[3]],t1[pos[2]]);
                for(int l=1;l<=n;l++){
                    int tot=judge(s[l],t1);
                    if(tot==0 && mx<2){
                        cout<<-1;return 0;
                    }
                    dif=max(dif,tot);
                }
                if(dif==2 || dif==0&&mx>=2){
                    cout<<(t1+1)<<'
';
                    return 0;
                }
                swap(t1[pos[3]],t1[pos[2]]);
                
                cout<<"

";
                cout<<-1<<'
';
                return 0;
            }
    }
    if(Max==4){
        for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
            if(judge(s[i],s[j])==4){
                memcpy(t1,s[i],sizeof s[i]);
                memcpy(t2,s[j],sizeof s[j]);
                int pos[5],id=0;
                for(int k=1;k<=K;k++)
                    if(t1[k]!=t2[k])pos[++id]=k;
                    
                for(int x=1;x<=4;x++)
                    for(int y=x+1;y<=4;y++){
                        swap(t1[pos[x]],t1[pos[y]]);
                        int dif=0;
                        for(int l=1;l<=n;l++){
                            int tot=judge(s[l],t1);
                            if(tot==0 && mx<2){
                                cout<<-1;return 0;
                            }
                            dif=max(dif,tot);
                        }
                        if(dif==2 || dif==0&&mx>=2){
                            cout<<(t1+1)<<'
';
                            return 0;
                        }
                        swap(t1[pos[x]],t1[pos[y]]);
                    }

                cout<<-1<<'
';
                return 0;
            }
    }
    
}
原文地址:https://www.cnblogs.com/zsben991126/p/12291407.html