置换环题目

F. Shifting String

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp found the string ss and the permutation pp. Their lengths turned out to be the same and equal to nn.

A permutation of nn elements — is an array of length nn, in which every integer from 11 to nn occurs exactly once. For example, [1,2,3][1,2,3] and [4,3,5,1,2][4,3,5,1,2] are permutations, but [1,2,4][1,2,4], [4,3,2,1,2][4,3,2,1,2] and [0,1,2][0,1,2] are not.

In one operation he can multiply ss by pp, so he replaces ss with string newnew, in which for any ii from 11 to nn it is true that newi=spinewi=spi. For example, with s=wmbes=wmbe and p=[3,1,4,2]p=[3,1,4,2], after operation the string will turn to s=s3s1s4s2=bwems=s3s1s4s2=bwem.

Polycarp wondered after how many operations the string would become equal to its initial value for the first time. Since it may take too long, he asks for your help in this matter.

It can be proved that the required number of operations always exists. It can be very large, so use a 64-bit integer type.

Input

The first line of input contains one integer tt (1≤t≤50001≤t≤5000) — the number of test cases in input.

The first line of each case contains single integer nn (1≤n≤2001≤n≤200) — the length of string and permutation.

The second line of each case contains a string ss of length nn, containing lowercase Latin letters.

The third line of each case contains nn integers — permutation pp (1≤pi≤n1≤pi≤n), all pipi are different.

Output

Output tt lines, each of which contains the answer to the corresponding test case of input. As an answer output single integer — the minimum number of operations, after which the string ss will become the same as it was before operations.

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=250;
int a[N],v[N];
char st[N];
ll gcd(ll a,ll b)
{
    return b?gcd(b,a%b):a;
}
void solve()
{
    int n;
    string s;
    cin>>n;
    cin>>s;
    for(int i=1;i<=n;i++) cin>>a[i];
    memset(v,0,sizeof v);
    ll res=1;
    for(int i=1;i<=n;i++)
    {
        if(!v[i])
        {
            int cnt=0;
            for(int j=i;!v[j];v[j]=1,j=a[j]) st[cnt++]=s[j-1];
            for(int k=1;k<=cnt;k++)
            {
                if(cnt%k==0)
                {
                    bool success=true;
                    for(int j=0;j<cnt;j++)
                    {
                        if(st[(j+k)%cnt]!=st[j])
                        {
                            success=false;
                            break;
                        }
                    }
                    if(success)
                    {
                        res=(res*k)/gcd(res,k);
                        break;
                    }
                }
            }
        }
    }
    cout<<res<<endl;
}
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        solve();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值