Cipher
Test instructions: First give the sequence of length n B[1...N], which represents the permutation relationship I, b[i], and then the input of the original character with this substitution relationship B to operate K times, ask the last string why?
Sample Input
104 5 3 7 2 8 1 6 CERC00 Hello Bob1995
Sample Output
Bolheol BC RCE
Idea: The topic meaning is obvious, after the direct cycle decomposition, in each cycle inside the direct mod + K can be, but this problem is the input. The string length is less than n to fill the empty space, and the string is immediately followed by a newline character;
Details: Use Get () to directly read K after the second to the end of the line (k after the space, will not be read, so do not need to getchar off first)
#include <iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<Set>#include<map>#include<queue>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#defineMSi (a) memset (A,0x3f,sizeof (a))#defineINF 0x3f3f3f3f#defineLson L, M, RT << 1#defineRson m+1, R, RT << 1|1#definePB Push_backtypedef __int64 Ll;template<typename t>voidRead1 (T &m) {T x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} M= x*F;} Template<typename t>voidRead2 (T &a,t &b) {Read1 (a); Read1 (b);} Template<typename t>voidRead3 (T &a,t &b,t &c) {Read1 (a); Read1 (b); Read1 (c);} Template<typename t>void out(T a) {if(a>9) out(ATen); Putchar (A%Ten+'0');}intb[ -],id[ -],index[ -];vector<int> vec[ -];Charans[ -],s[ -];intMain () {intn,cnt; while(Read1 (n), N) {rep1 (I,1, N) read1 (B[i]); MS0 (ID); CNT=0; REP1 (i,1, N)if(!Id[i]) {CNT++; intTMP = I,d =0; if(!vec[cnt].empty ()) vec[cnt].clear (); Do{id[tmp]=CNT; INDEX[TMP]= d++; VEC[CNT].PB (TMP); TMP=B[tmp]; } while(!id[tmp]); } intK; while(Read1 (k), K) {gets (s+1);//if (gets (s+1) = = NULL) puts ("bug");Rep1 (I,strlen (s+1)+1, n) s[i] =' '; //rep1 (i,1,n) cout<<s[i];REP1 (I,1, N) {ans[vec[id[i]][(Index[i]+k)%vec[id[i]].size () []] =S[i]; } rep1 (I,1, N) putchar (ans[i]); Puts (""); } puts (""); } return 0;}
View Code
POJ 1026 Cipher