E - Substring Reverse

Two strings s and t of the same length are given. Determine whether it is possible to make t from s using exactly one reverse of some its substring.

Input
The first line contains the string s, and the second — the string t. Both strings have the same length from 1 to 200000 characters and consist of lowercase Latin letters.

Output
Output «YES», if it is possible to reverse some substring of s to make s equal to t, and «NO», otherwise.

Examples
Input
abcdefg
abedcfg
Output
YES
Input
abcdefg
abdecfg
Output
NO
思路:從前開始找到不一樣字母的下標,再從後找到不一樣字母的下標。

#include<bits/stdc++.h>
using namespace std;
const int maxx=2e5+10;
char a[maxx],b[maxx];
int main()
{
    std::ios::sync_with_stdio(false);
    cin>>a>>b;
    int len=strlen(a);
    int n1=-1,n2=-1;
    for(int i=0; i<len; i++)
    {
        if(a[i]!=b[i])
        {
            n1=i;
            break;
        }
    }
    for(int i=len-1; i>=0; i--)
    {
        if(a[i]!=b[i])
        {
            n2=i;
            break;
        }
    }
    int flag=1;
    for(int i=n1; i<=n2; i++)
    {
        if(a[i]!=b[n2+n1-i])
        {
            flag=0;
            // cout<<i<<"&&&&"<<j<<endl;
            break;
        }
    }
    if(flag==1)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章