标签:ini repr 循环 algorithm scan value size desc color
Problem Description
InputThe input contains several test cases. Each test case starts with a line containing two numbers n and m (1 ≤ n ≤ 80, 1 ≤ m ≤ 109). The following line consists of n pairwise different numbers p1,...,pn (1 ≤ pi ≤ n). The third line of each test case consists of exactly n characters, and represent the encoded string. The last test case is followed by a line containing two zeros.
OutputFor each test case, print one line with the decoded string.
Sample Input
5 3 2 3 1 5 4 helol 16 804289384 13 10 2 7 8 1 16 12 15 6 5 14 3 4 11 9 scssoet tcaede n 8 12 5 3 4 2 1 8 6 7 encoded? 0 0
Sample Output
hello second test case encoded?
这题是寻找循环节。(跟他所在的题单好像完全没关系啊)
循环节就是重复一定次数后达到一种循环的状态。由于字符串的转换关系是不变的。固必然会出现循环。(具体论证懒得算了。鸽巢定理(吧))
寻找字符串的循环节比较困难且复杂且耗时。由于每个字符串最多只有80个字符。故我们可以对字符串内每一个字符寻找其循环节即可。
AC代码如下:
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; int n,m,p[85],temp,pos[85][85],cnt; char s[85],ans[85]; int main(){ while(scanf("%d%d",&n,&m),n){ for(int i=0;i<n;i++){ scanf("%d",&p[i]); p[i]--; } getchar(); gets(s); for(int i=0;i<n;i++){ cnt=0; temp=p[i]; pos[i][cnt++]=i; while(temp!=i){ pos[i][cnt++]=temp;//记录i在第cnt次循环在 什么位置 temp=p[temp]; } ans[pos[i][m%cnt]]=s[i]; } ans[n]=‘\0‘; printf("%s\n",ans); } }
2020 BIT冬训-二分三分快速幂矩阵 L - Decode the Strings HDU - 2371
标签:ini repr 循环 algorithm scan value size desc color
原文地址:https://www.cnblogs.com/mikku39/p/14409734.html