POJ 1026 Cipher(置换群)
题意 :由n个数字组成的密钥,每个数字都不相同,都在1~n之间,有一份长度小于等于n的信息,要求将信息放到密钥下边,一一对应,信息不足n的时候补空格,然后将位置重新排列,将此过程重复k次,求最后的字符串序列,最后的空格不用输出。
思路 :如果按照最原始的求循环结的话会超时,因为k值范围很大。所以要用置换群,把每个的元素的循环求出来,直接对其取余即可。会3270的话,这个题理解起来挺容易的。
//
#include <stdio.h>
#include <string.h>
#include <iostream> using namespace std ; int a[],ch[] ;
char sh[] ,pri[]; void xh(int n)
{
for(int i = ; i <= n ; i++)
{
int cnt = ;
int pos = a[i] ;
while(pos != i)
{
pos = a[pos] ;
cnt ++ ;
}
ch[i] = cnt ;
}
}
int main()
{
int n,k ;
while(~scanf("%d",&n) && n)
{
memset(a,,sizeof(a)) ;
for(int i = ; i <= n ; i++)
scanf("%d",&a[i]) ;
//memset(sh,0,sizeof(sh)) ;
xh(n) ;
while(~scanf("%d",&k) && k)
{
getchar() ;
gets(sh+) ;
for(int i = strlen(sh+) + ; i <= n ; i++ )
sh[i] = ' ' ;
sh[n+] = '\0' ;
for(int i = ; i <= n ; i++)
{
int pos = i ;
for(int j = ; j < k % ch[i] ; j ++)
pos = a[pos] ;
pri[pos] = sh[i] ;
}
pri[n+] = '\0' ;
for(int i = ; i <= n ; i++)
printf("%c",pri[i]) ;
puts("") ;
}
puts("") ;
}
return ;
}
POJ 1026 Cipher(置换群)的更多相关文章
- poj 1026 Cipher
置换群就可以搞定!!! 注意下格式就好了…… #include<iostream> #include<stdio.h> #include<algorithm> #i ...
- POJ 1026 Cipher(更换)
Cipher Time Limit: 1000MS Memory Li ...
- poj 1026(置换群)
题意:给你一个变换规则,和一个字符串,问经过k次变换后得到的字符串. 思路:开始的时候试图去找它的整个周期,谁知道周期太大了,各种RE,后来在得知此题需要用置换群来优化,第一次接触置换群学习了下! 代 ...
- 【POJ】1026.Cipher
题解 置换群的快速幂,然而我姿势水平不高,样例过不去,然后才明白这个置换的意思是这个位置上的数代表要把原位置的某个数换过来 需要新开一个数组存结果 代码 #include <iostream&g ...
- POJ 1026 置换群的k次幂问题
题目大意: 给定了一组对应关系,经过k次幂后,得到新的对应关系b[i],然后将给定的字符串上的第i位字符放置到b[i]的位置上, 如果字符串长度不足n就用空格补足,这里的是空格,也就是str[i] = ...
- poj1026 Cipher ——置换群
link:http://poj.org/problem?id=1026 其实这道题目和poj2369这道题目一样. 都是基础的置换群题目.把那道题目理解了,这道题就没问题了. 不过我的方法貌似比较挫, ...
- Cipher(置换群)
Cipher Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20821 Accepted: 5708 Descripti ...
- poj3270 && poj 1026(置换问题)
| 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...
- poj 1721 CARDS (置换群)
题意:给你一个数列,第i号位置的数位a[i],现在将数列进行交换,交换规则为a[i]=a[a[i]]:已知交换s次之后的序列,求原先序列 思路:置换的问题必然存在一个循环节,使一个数列交换n次回到原来 ...
随机推荐
- 伴随ListView、RecyclerView、ScrollView滚动滑入滑出小图标--第三方开源--FloatingActionButton
FloatingActionButton在github上的项目主页是:https://github.com/makovkastar/FloatingActionButton 它的依赖包NineOldA ...
- WPF学习06:转换控件内容为可存储图片
在图形软件中,我们经常使用到"另存为图片"的功能,本文即介绍如何将WPF控件显示内容转换为图片. , , PixelFormats.Pbgra32); bitmapRender.R ...
- Java从入门到精通——调错篇之ORACLE 打开PLSQL时提示ora-01033
客户Oracle服务器进入PL/SQL Developer时报ora-01033:oracle initializationg or shutdown in progress 错误提示,应用系统无法连 ...
- db2查看表空间
select substr(tbsp_name,1,20) as 表空间名称,substr(tbsp_content_type,1,10) as 表空间类型,sum(tbsp_total_size_k ...
- 升级 CentOS git 1.7.1 到 1.7.12
CentOS 源里的 git 版本是 1.7.1,如果远程创建的库所用 git 的版本比它高,在 pull 的时候,如果本地有修改,就会永久阻塞:在 push 的时候就会失败. CentOS 源里的 ...
- thinkphp通行证服务,验证登录,注销登录
<?php /** * 通行证服务 */ class PassportService extends Service { /** * 验证用户或者管理员是否已登录 * @return boole ...
- (菜鸟要飞系列)一,基于Asp.Net MVC5的后台管理系统(前言)
今天真是个郁闷的日子,因为老师两个星期前给我的一个任务,用递归算法将Oracle数据库中用户信息及权限显示在jquery-treeView上,网上虽然有大神写出了这类算法,但是不贴全部代码,真的很难跟 ...
- left join 过滤条件写在on后面和写在where 后面的区别
create table t1(id int, feild int);insert into t1 values(1 , 1);insert into t1 values(1 , 2);insert ...
- bnuoj 33656 J. C.S.I.: P15(图形搜索题)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=33656 [题解]:暴力搜索题 [code]: #include <iostream> # ...
- Start my cnBlogs
Compared to CSDN blog, althought it's my first time to use CNBlog,i felt it makes me more comfortabl ...