1026:Cipher

  • 查看
  • 提交
  • 统计
  • 提示
  • 提问
总时间限制: 
1000ms 
内存限制: 
65536kB
描述
Bob and Alice started to use a brand-new encoding scheme. Surprisingly it is not a Public Key Cryptosystem, but their encoding and decoding is based on secret keys. They chose the secret key at their last meeting in Philadelphia on February 16th, 1996. They chose as a secret key a sequence of n distinct integers, a1 ; . . .; an, greater than zero and less or equal to n. The encoding is based on the following principle. The message is written down below the key, so that characters in the message and numbers in the key are correspondingly aligned. Character in the message at the position i is written in the encoded message at the position ai, where ai is the corresponding number in the key. And then the encoded message is encoded in the same way. This process is repeated k times. After kth encoding they exchange their message.

The length of the message is always less or equal than n. If the message is shorter than n, then spaces are added to the end of the message to get the message with the length n.

Help Alice and Bob and write program which reads the key and then a sequence of pairs consisting of k and message to be encoded k times and produces a list of encoded messages.

输入
The input file consists of several blocks. Each block has a number 0 < n <= 200 in the first line. The next line contains a sequence of n numbers pairwise distinct and each greater than zero and less or equal than n. Next lines contain integer number k and one message of ascii characters separated by one space. The lines are ended with eol, this eol does not belong to the message. The block ends with the separate line with the number 0. After the last block there is in separate line the number 0.
输出
Output is divided into blocks corresponding to the input blocks. Each block contains the encoded input messages in the same order as in input file. Each encoded message in the output file has the lenght n. After each block there is one empty line.
样例输入
10
4 5 3 7 2 8 1 6 10 9
1 Hello Bob
1995 CERC
0
0
样例输出
BolHeol  b
C RCE
来源
Central Europe 1995
相当于用一个置换作用,重复多次,把字符串打乱.求出每个数所在的轮换的大小,用k取模之后便知道最少作用多少次,求出变换之后的位置即可.
  • #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<iomanip>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<set>
    #include<map>
    using namespace std;
    int a[205];
    bool vis[205];
    int num[205];
    int Map[205];
    char s[205];
    int main()
    {int n,k;while(cin>>n&&n) {for(int i=1;i<=n;++i){cin>>a[i];}memset(vis,0,sizeof(vis));for(int i=1;i<=n;++i){if(vis[i])continue;vis[i]=true;int tmp=1;for(int j=a[i];j!=i;j=a[j]){tmp++;vis[j]=true;} num[i]=tmp;for(int j=a[i];j!=i;j=a[j]){num[j]=tmp;}}while(cin>>k&&k){getchar();gets(s+1);int len=strlen(s+1);for(int i=len+1;i<=n;++i){s[i]=' ';}s[n+1]='\0';for(int i=1;i<=n;++i){int j=i;for(int t=0;t<(k%num[i]);++t){j=a[j];}Map[j]=i;}for(int i=1;i<=n;++i){cout<<s[Map[i]];}cout<<endl;}cout<<endl;}return 0;
    }
    

poj 1026:Cipher相关推荐

  1. POJ - 1026 Cipher(置换群的幂)

    题目链接:点击查看 题目大意:给出一个置换群,再给出数个字符串,每个字符串需要输出执行 k 次置换群后的答案 题目分析:置换群的 k 次幂,直接写就行了 代码: #include<iostrea ...

  2. POJ 1026 Cipher G++

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; //抄博 ...

  3. 【POJ】1026.Cipher

    题解 置换群的快速幂,然而我姿势水平不高,样例过不去,然后才明白这个置换的意思是这个位置上的数代表要把原位置的某个数换过来 需要新开一个数组存结果 代码 #include <iostream&g ...

  4. POJ前面的题目算法思路【转】

    1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...

  5. POJ 超详细分类

    POJ 各题算法 1000    A+B Problem            送分题     49%    2005-5-7 1001    Exponentiation         高精度   ...

  6. ajax怎样带值,ajax携带状态值

    linux下python调用c模块 在C调用Python模块时需要初始化Python解释器,导入模块等,但Python调用C模块却比较简单,下面还是以helloWorld.c 和 main.py 做一 ...

  7. poj题目详细分类及算法推荐题目

    DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  1024   Calendar Game       简单题  ...

  8. ACM POJ 题目分类(完整整理版本)

    DP: 1011   NTA                 简单题  1013   Great Equipment     简单题  1024   Calendar Game       简单题   ...

  9. POJ ZOJ题目分类

    POJ,ZOJ题目分类(多篇整合版,分类很细致,全面) 标签: 题目分类POJ整理 2015-04-18 14:44 1672人阅读 评论(0) 收藏 举报 本文章已收录于: 分类: ACM资料(5) ...

最新文章

  1. 【解决方法】Panda read_csv()把第一行的数据变成了列名,怎么处理
  2. 多表查询返回多个DataTable,合并到一个Table中.
  3. DNN 汉化中的问题????
  4. IXMLDOMDocument中的load方法返回值有BUG
  5. CentOS7 如何关闭防火墙
  6. 涨姿势 | 如何修复硬盘,以及如何避免硬盘损坏
  7. 为什么读大学时做学术比搞项目重要?
  8. sql聚合函数及分组的注意事项
  9. 超详细正点原子STM32F429开发板视频教程笔记01
  10. 深度学习软件安装及环境配置(Win10)
  11. 请你说一说红黑树的性质还有左右旋转
  12. Django 解决CSRF 跨域问题总结
  13. 婚姻家庭法简答题及答案
  14. mc服务器物品展示,我的世界物品展示框怎么做
  15. 基于mysql的应用程序设计[j] 兰旭辉_熊家军_邓刚_高校C语言自动考试系统参考文献...
  16. shell程序设计小知识
  17. spark on hive 的部署,和spark on hive (ha)在本地测试步骤
  18. 《华尔街》观后笔记9——拯救危机
  19. java junit 覆盖率_java单元测试篇:使用clover为junit单元测试做覆盖率分析(二)...
  20. 分区数据恢复之ubuntu13.10格式化整个硬盘

热门文章

  1. 学校计算机室管理员岗位职责,微机室管理员有哪些岗位职责
  2. js中单引号和双引号的区别
  3. 如何获得邻居家WiFi密码(实用)
  4. gcc环境查看汇编代码,二进制代码。
  5. html期末作业代码网页设计 web网页设计实例作业 ——中国风文化传媒企业官网(6页) 简单网页设计作业 静态HTML文化主题网页作业
  6. BP神经网络算法在MATLAB中的代码
  7. UE 使用时间轴Timeline来实现动画效果
  8. 毫无基础的人如何入门Python这个编程语言?
  9. 铁路上海站各区售票网点分布表
  10. 电梯仿真程序设计报告+任务书+仿真电梯MFC源程序