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 and do a cyclic shift on that subsequence exactly once.

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 (1 ≤ n ≤ 105) specifying the length of strings a and b. Then two lines follow, giving strings a and b, respectively. Both strings consist only of lowercase English letters.

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值