K. Cyclic Shift(思維)
K. Cyclic Shift
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output
You are given two strings a and b of the same length and consisting of lowercase English letters. You can pick at most one subsequence of string b
For example, if you have a string "abcdefg" and you picked the letters at indices 2, 5, and 6 as a subsequence to do a cyclic shift on them, the letter at index 2 will go to index 5, the letter at index 5 will go to index 6, the letter at index 6 will go to index 2, and the string will become "afcdbeg".
Your task is to check if it is possible to make string b equivalent to string a using at most one cyclic shift. Can you?
Input
The first line contains an integer T (1 ≤ T ≤ 200) specifying the number of test cases.
The first line of each test case contains an integer n
Output
For each test case, print a single line containing "YES" (without quotes) if it is possible to make string b equivalent to string a using at most one cyclic shift. Otherwise, print "NO" (without quotes).
Example
input
Copy
2 6 abcdef adcebf 4 abcd dabd
output
Copy
YES NO
Note
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. For example, the sequence is a subsequence of obtained after removal of elements , , and .
#include<bits/stdc++.h>
using namespace std;
vector<int>q;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
string a,b;
cin>>a>>b;
q.clear();
for(int i=0; i<n; i++)
{
if(a[i]!=b[i])
{
q.push_back(i);
}
}
int len=q.size();
bool f=true;
for(int i=0; i<len; i++)
{
if(b[q[i]]!=a[q[(i+1)%len]])
{
f=false;
break;
}
}
if(f)
puts("YES");
else
puts("NO");
}
return 0;
}