codeforces 527B:瞎搞

#include"cstdio"
#include"queue"
#include"cmath"
#include"stack"
#include"iostream"
#include"algorithm"
#include"cstring"
#include"queue"
#include"map"
#include"vector"
#define ll long long
#define mems(a,b) memset(a,b,sizeof(a))
#define ls pos<<1
#define rs pos<<1|1

using namespace std;
const int MAXN = 200500;
const int MAXE = 200500;
const int INF = 0x3f3f3f3f;

int mat[26][26];

int main(){
    int len,ans=0;
    char a[MAXN],b[MAXN];
    scanf("%d",&len);
        scanf("%s %s",a,b);
        mems(mat,0);
        for(int i=0;i<len;i++){
            if(a[i]!=b[i]) {mat[a[i]-'a'][b[i]-'a']=i+1;ans++;}
        }
        for(int i=0;i<26;i++)
        for(int j=0;j<26;j++){
            if(mat[i][j]&&mat[j][i]){
                cout<<ans-2<<endl;
                cout<<mat[i][j]<<' '<<mat[j][i]<<endl;
                return 0;
            }
        }
        for(int i=0;i<26;i++)
        for(int j=0;j<26;j++){
            if(!mat[i][j]) continue;
            for(int k=0;k<26;k++){
                if(mat[j][k]){
                    cout<<ans-1<<endl;
                    cout<<mat[i][j]<<' '<<mat[j][k]<<endl;
                    return 0;
                }
                if(mat[k][i]){
                    cout<<ans-1<<endl;
                    cout<<mat[i][j]<<' '<<mat[k][i]<<endl;
                    return 0;
                }
            }
        }
    cout<<ans<<endl;
    cout<<"-1 -1"<<endl;

    return 0;
}

 

题意大概是说给两个字符串,可以把第一个字符串的任意两个字母互换,问最少有多少个字符不一样

字符串的 长度是200000,但是只有26个字母

定义了个数组:mat[i][j]=p表示第一个字符串中的i字符和第二个字符串中的j字符在p位置不一样

这样如果有一组mat[j][i],那这是最好的情况

如果没有这样的情况,那就寻找有没有mat[i][j]和mat[j][k]或者是mat[i][j]和mat[k][i]这样的情况

如果还是没有那输出-1

 

posted @ 2016-01-17 18:55  Septher  阅读(219)  评论(0编辑  收藏  举报