USC 1329 Decode 坑坑坑gets


此页面通过工具从 csdn 导出,格式可能有问题。


其实一贯都是用string来着。。

其实后来看刘汝佳的书又喜欢上了fgets然后一路过关斩将来着。。


这次觉得题水就用了gets啊啊啊啊

然后就WA了9次啊有木有!!!


H. Decode

TimeLimit: 1s   MemoryLimit: 64M

Description

Bruce Force has had an interesting idea how to encode strings. The following is the description of how the encoding is done:
Let x 1 ,x 2 ,...,x n be the sequence of characters of the string to be encoded.
1.Choose an integer m and n pairwise distinct numbers p 1 ,p 2 ,...,p n from the set { 1 , 2 , ..., n } (a permutation of the numbers 1 to n ).
2.Repeat the following step m times.
3.For 1 ≤ i ≤ n set y i to x pi , and then for 1 ≤ i ≤ n replace x i by y i .
For example, when we want to encode the string "hello", and we choose the value m = 3 and the permutation 2, 3, 1, 5, 4 , the data would be encoded in 3 steps: "hello" - > "elhol" -> "lhelo" -> "helol".
Bruce gives you the encoded strings, and the numbers m and p 1 , ..., p n used to encode these strings. He claims that because he used huge numbers m for encoding, you will need a lot of time to decode the strings. Can you disprove this claim by quickly decoding the strings?

Input

The input contains several test cases. Each test case starts with a line containing two numbers n and m ( 1 ≤ n ≤ 80 , 1 ≤ m ≤ 10 9 ). The following line consists of n pairwise different numbers p 1 ,...,p n ( 1 ≤ p i 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 cont aining two zeros.

Output

For each test case, print one line with the decoded string.

SampleInput

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

SampleOutput

hello
second test case
encoded?


不说啥了,就是mod一个周期就行。getline的代码在最下面,纪念一下死的多坑。


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#define LL long long

using namespace std;

const int INF = 999999999; const double eps = 1e-6;

int T,I,n,m,t; LL tt, sum; int a[1111]; int f[1111]; int dp[888][888]; char s[888]; char ans[888]; char ss[888];

int so(int v){ if (a[v] == v ) return 1; int t = a[v]; int res = 1; while (t!=v){ t = a[t]; res++; } return res; }

int too(int v,int k){ for (int i(1);i<=k;i++){ v = a[v]; } return v; }

int main(){ while ( fgets(ss, INF, stdin) ) { sscanf(ss, “%d%d”,&n,&m); if (n==0 && m==0) break;

    fgets(ss, INF, stdin);
    char *p = strtok(ss, " ");
    for (int i(1);i&lt;=n;i++) {
        sscanf(p, "%d", &amp;a[i]);
        p = strtok(NULL, " ");
        f[i] = 0;
    }

    for (int i(1);i&lt;=n;i++) {
        f[i] = so(i);
    }

    fgets(s+1, INF, stdin);

    for ( int i(1); i&lt;=n; i++) {
        ans[too(i, m%f[i] )] = s[i];
    }
    ans[n+1] = 0;
    printf("%s\n",ans+1);
}
return 0;

}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
using namespace std;
const int MAXN = 888;
int n, m;
int a[MAXN], f[MAXN];
char ans[MAXN], s[MAXN], ch;

int to(int v, int w){ while( w– ) v = a[v]; return v; }

int main(){
while( EOF != scanf("%d %d\n", &n, &m) ){ if( !n && !m ) break; //getchar(); for(int i=1; i<=n; i++) cin>>a[i];//scanf("%d", &a[i]), getchar(); cin.getline(s+1, MAXN); cin.getline(s+1, MAXN); for(int i=1; i<=n; i++) if( a[i]==i ) f[i] = 1; else{ int t=a[i], cnt=1; while( t!=i ) t=a[t], cnt++; f[i] = cnt; } for(int i=1; i<=n; i++) ans[ to(i, m%f[i]) ] = s[i]; ans[n+1] = 0; cout<<ans+1<<endl; } return 0; }


真切感慨以后在时间充裕的时候能用C++就用C++。别自作多情的装逼用c啥啥的


Avatar
huiren
Code Artisan

问渠那得清如许,为有源头活水来

下一页
上一页
comments powered by Disqus