E. Substring Reverse

E. Substring Reverse
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output

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
Copy
abcdefg
abedcfg
output
Copy
YES
input
Copy
abcdefg
abdecfg
output
Copy
NO

題意:把第二個串的一個段 顛倒能使兩個串相同就輸出YES,否者就輸出NO

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<map>
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
#include<ctime>
#include<stack>
#define PI 3.14159265358797832384626
using namespace std;
int main()
{
    char str1[200020],str2[200020],str3[200020];
    while (~scanf("%s%s",str1,str2))
    {
        if(strcmp(str1,str2)==0)
        {
            printf("YES\n");
            continue;
        }
        int l,f,len=strlen(str1);
        for(int i=0; i<len; i++)
        {
            if(str1[i]!=str2[i])
            {
                l=i;
                break;
            }
        }
        for(int i=len-1; i>=0; i--)
        {
            if(str1[i]!=str2[i])
            {
                f=i;
                break;
            }
        }
        strcpy(str3,str1);
        for(int i=l,j=f;i<=f;i++,j--)
        {
            str3[i]=str1[j];
        }
        if(strcmp(str3,str2)==0)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

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