Description:
跟你两个不相同的字符串,问你能否将第一个字符串任意两个字母交换一次使得两字符串相同,YES or NO
Solution:
一维模拟就好了
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 1e5 + 1e3;
char s1[maxn],s2[maxn];
int main()
{
while(~scanf("%s%s",s1,s2))
{
int flag = 1;
int len1 = strlen(s1);
int len2 = strlen(s2);
int cnt = 0;
char change_ch1[5];
char change_ch2[5];
if(len1 != len2)flag = 0; for(int i = 0;i < len1 && flag ;++i)
{
if(s1[i] != s2[i])
{
change_ch1[++cnt] = s1[i];
change_ch2[cnt] = s2[i];
}
if(cnt > 2)flag = 0;
}
if(cnt == 1)flag = 0;
if(cnt == 2 && change_ch1[1] == change_ch2[2] && change_ch1[2] == change_ch2[1])
flag = 1;
else
flag = 0;
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}