Description
给出两个字符串 a,b a , b ,问把 b b 的一个子串反转是否可以把变成 a a
Input
两个等长的串长不超过的字符串 a,b a , b
Output
如果可以通过反转 b b 的一个子串把变成 a a 则输出,否则输出 NO N O
Sample Input
abcdefg
abedcfg
Sample Output
YES
Solution
两个串已经相同显然有解,否则从前往后找到两个字符串第一个不相同的位置 l l ,从后往前找到两个字符串第一个不相同的位置,如果 b b 串的段反转与 a a 串的段相同则有解
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=200005;
char a[maxn],b[maxn];
int main()
{
scanf("%s%s",a+1,b+1);
int n=strlen(a+1);
int l=1,r=n;
while(l<=n&&a[l]==b[l])l++;
while(r&&a[r]==b[r])r--;
int flag=1;
while(l<=r)
{
if(a[l]!=b[r]||a[r]!=b[l])
{
flag=0;
break;
}
l++,r--;
}
printf("%s\n",flag?"YES":"NO");
return 0;
}