其实一贯都是用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 nand 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.
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 0SampleOutput
hello second test case encoded?
不说啥了,就是mod一个周期就行。getline的代码在最下面,纪念一下死的多坑。
#include
#include #include #include #include #include #include #include #include #include #include #include
#include
#include #include #include 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<
真切感慨以后在时间充裕的时候能用C++就用C++。别自作多情的装逼用c啥啥的