您的位置:首页 > 其它

水题

2018-02-15 21:08 197 查看
Some dwarves that are finishing the StUDY (State University for Dwarven Youngsters) Bachelor courses, have been told "no genome, no degree". That means that all dwarves should write a thesis on genome. Dwarven genome is far from simple. It is represented by a string that consists of lowercase Latin letters.
Dwarf Misha has already chosen the subject for his thesis: determining by two dwarven genomes, whether they belong to the same race. Two dwarves belong to the same race if we can swap two characters in the first dwarf's genome and get the second dwarf's genome as a result. Help Dwarf Misha and find out whether two gnomes belong to the same race or not.
InputThe first line contains the first dwarf's genome: a non-empty string, consisting of lowercase Latin letters.
The second line contains the second dwarf's genome: a non-empty string, consisting of lowercase Latin letters.
The number of letters in each genome doesn't exceed 105. It is guaranteed that the strings that correspond to the genomes are different. The given genomes may have different length.
OutputPrint "YES", if the dwarves belong to the same race. Otherwise, print "NO".
ExampleInput
ab
ba
Output
YES
Input
aa
ab
Output
NO
NoteFirst example: you can simply swap two letters in string "ab". So we get "ba".
Second example: we can't change string "aa" into string "ab", because "aa" does not contain letter "b".
此题就是判断一个字符串能否通过调整其中两个字母的顺序变成另外一个字符串,可以先统计原始两个字符串中不一样的地方,看看是否只有两个位置字母不同,然后看是否相同。
#include<stdio.h>
#include<string.h>
#define N 100010
int main()
{
char a
,b
,x[10],y[10];
int c=0,i;
long int m,n;
gets(a);
gets(b);
m=strlen(a);
n=strlen(b);
if(m!=n)
{
printf("NO\n");
return 0;
}
for(i=0;i<m;i++)
{
if(c==3)
{
break;
}
if(a[i]!=b[i])
{
x[c]=a[i];
y[c]=b[i];
c++;
}
}
if(c==2&&x[0]==y[1]&&y[0]==x[1])
{
printf("YES\n");
}
else printf("NO\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: