poj 1026 Cipher

Cipher

题意:先给长度为n的序列B[1...n],表示置换关系 i -> B[i];之后对输入的原字符用这个置换关系B操作k次,问最后的字符串为什么?

Sample Input

10
4 5 3 7 2 8 1 6 10 9
1 Hello Bob
1995 CERC
0
0

Sample Output

BolHeol  b
C RCE

思路:题目意思很明显,直接循环分解之后,在每一个循环里面直接mod + k即可;但是这道题坑的是输入。。字符串长度小于n要自己补空位,并且字符串后面直接就是换行符了;
细节: 使用gets()来直接读取k后第二个到行尾;(k后面的空格,不会被读取,所以不需要先getchar掉)

#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 namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1|1
#define pb push_back
typedef __int64 ll;
template<typename T>
void read1(T &m)
{
    T x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
    if(a>9) out(a/10);
    putchar(a%10+'0');
}
int B[220],id[220],index[220];
vector<int> vec[220];
char ans[220],s[220];
int main()
{
    int n,cnt;
    while(read1(n),n){
        rep1(i,1,n) read1(B[i]);
        MS0(id);cnt = 0;
        rep1(i,1,n)if(!id[i]){
            cnt++;
            int tmp = 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]);
        }
        int k;
        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
原文地址:https://www.cnblogs.com/hxer/p/5223208.html