5.29/C

来源:互联网 发布:影响淘宝权重的因素 编辑:程序博客网 时间:2024/04/29 16:54

Description

Ford Prefect got a job as a web developer for a small company that makes towels. His current work task is to create a search engine for the website of the company. During the development process, he needs to write a subroutine for comparing strings S and T of equal length to be "similar". After a brief search on the Internet, he learned about the Hamming distance between two strings S and T of the same length, which is defined as the number of positions in which S and T have different characters. For example, the Hamming distance between words "permanent" and "pergament" is two, as these words differ in the fourth and sixth letters.

Moreover, as he was searching for information, he also noticed that modern search engines have powerful mechanisms to correct errors in the request to improve the quality of search. Ford doesn't know much about human beings, so he assumed that the most common mistake in a request is swapping two arbitrary letters of the string (not necessarily adjacent). Now he wants to write a function that determines which two letters should be swapped in string S, so that the Hamming distance between a new string S and string T would be as small as possible, or otherwise, determine that such a replacement cannot reduce the distance between the strings.

Help him do this!

Input

The first line contains integer n (1 ≤ n ≤ 200 000) — the length of strings S and T.

The second line contains string S.

The third line contains string T.

Each of the lines only contains lowercase Latin letters.

Output

In the first line, print number x — the minimum possible Hamming distance between strings S and T if you swap at most one pair of letters in S.

In the second line, either print the indexes i and j (1 ≤ i, j ≤ ni ≠ j), if reaching the minimum possible distance is possible by swapping letters on positions i and j, or print "-1 -1", if it is not necessary to swap characters.

If there are multiple possible answers, print any of them.

Sample Input

Input
9pergamentpermanent
Output
14 6
Input
6wookiecookie
Output
1-1 -1
Input
4petregor
Output
21 2
Input
6doublebundle
Output
24 1

Hint

In the second test it is acceptable to print i = 2j = 3.

题目的意思是给出两个长度为N的串,在两个串中,对应位置的字母有可能不同。不同则累加1。也可以通过替换字母位置,来减少累加值。若替换后两个位置的字母都变的相同,则累加值减去2.输出结果,并输出替换的两个位置编号。也可能替换后只有一个相同,则减去一。输出结果,输出位置编号。若无法通过替换位置来减少累加值,则输出结果,并输出-1 -1.

这题在赛时是没做出来的,时间1个半小时,真的很佩服那帮神牛们可以再这么短的时间内做出来。赛后我也是用了1个小时才做完啊。。。啊啊。。

首先扫一遍两个串,找出不同的个数。并用二维数组记录位置i两个串中字母都是什么。然后先找是否有可以减少2的替换方法,即两层循环搜索各种可能,当记录下来的位置i处两个字母不同,即used[i][j]!=0 && i!=j且有对应的used[j][i]!=0则表明可以替换,记录位置,然后输出结果即可。若无这种可能,则需寻找是否替换一次可以减少1。两层循环,先找到一个used[i][j]!=0 && i!=,然后开启另一层循环,寻找used[k][i]!=0 && k!=i 或used[j][k]!=0 && j!=k的情况,若有便记录位置,退出查找,输出结果。若最终没有可替换项,则输出-1 -1.

代码如下

这里有一点需要注意,在找可以减1这种情况时,需要最后的位置编号i,j满足i<=j.

#include <cstdio>#include <cstring>#include<algorithm>using namespace std;char str1[200001],str2[200001];int main(){    int n,p,set1,set2,ans;    int used[30][30];    memset(used,0,sizeof(used));    scanf("%d",&n);    scanf("%s",str1);    scanf("%s",str2);    ans=0;    set1=-1;    set2=-1;    for (int i=0;i<n;i++)    {       if (str1[i]!=str2[i]) ans++;       used[str1[i]-'a'][str2[i]-'a']=i+1;    }    //printf("%d\n",ans);    for (int i=0;i<26;i++)    {      p=1;      for (int j=0;j<26;j++)        if (used[i][j]!=0 && used[j][i]!=0 && i!=j)        {            set1=used[i][j];            set2=used[j][i];            p=0;            break;        }      if(p==0) break;    }    if (set1>0)    {        printf("%d\n",ans-2);        printf("%d %d\n",set1,set2);        return 0;    }    for (int i=0;i<26;i++)    {        p=1;        for (int j=0;j<26;j++)          if (used[i][j]!=0 && i!=j)            for (int k=0;k<26;k++)            {              if (k!=i && used[k][i]!=0)              {                  set1=used[i][j];                  set2=used[k][i];                  p=0;                  break;              }              if (k!=j && used[j][k]!=0)              {                  set1=used[i][j];                  set2=used[j][k];                  p=0;                  break;              }            }        if (p==0) break;    }    if (set1>0)    {        printf("%d\n",ans-1);        printf("%d %d\n",set1,set2);        return 0;    }    printf("%d\n",ans);    printf("-1 -1\n");    return 0;}

0 0
原创粉丝点击