poj1026--Cipher
来源:互联网 发布:天刀天香萝莉捏脸数据 编辑:程序博客网 时间:2024/05/17 02:20
题意:看着样例说吧 给你n个数字a[i](>0&&<=n),再给你交换次数k,之后是一串字符串;如果字符串长度小于n,后面补为空格;然后进行交换:之前字符串的位置i对应的 a[i]位置就是交换一次后的字符位置,问交换k次后的字符串;
分析:单纯模拟会超时,这其中有个规律:对于一个字符,交换某些次之后,就会变回原来的(即会循环),只需找出它的循环周期T,交换k%T次就可;
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stdlib.h>
#define N 10010
#define INF 10000000
#define eps 10E-9
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
int a[N];
int a_circle[N];
char str[N];
char stre[N];
char s[N];
int circle(int x)//找周期
{
int result=1,k=a[x];
while(1)
{
if(x==k)
return result;
k=a[k];
result++;
}
}
int main()
{
int n,k,i,j;
while(~scanf("%d",&n)&&n)
{
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
a[i]=a[i]-1;
}
while(~scanf("%d",&k)&&k)
{
getchar();
gets(str);
int len=strlen(str);
if(len<n)
{
for(i=len; i<n; i++)
str[i]=' ';
str[i]='\0';
}
for(i=0; i<n; i++)
{
a_circle[i]=circle(i);
}
char c;
for(i=0; i<n; i++)
{
int temp=k%a_circle[i];
int d=i;
// cout<<k<<endl;
while (temp--)//交换
d = a[d];
stre[d]=str[i];
}
for(i=0; i<n; i++)
printf("%c",stre[i]);
printf("\n");
}
printf("\n");
}
return 0;
}
- poj1026--Cipher
- POJ1026 Cipher
- POJ1026 HDU1439 Cipher
- POJ1026,Cipher,数学题
- POJ1026 Cipher 【polya】
- poj1026 Cipher 置换群
- poj1026 Cipher (循环节)
- POJ1026 HDU1439 Cipher【置换群】
- poj1026--Cipher(置换群)
- POJ1026
- poj1026
- poj1026
- poj1026
- poj1026
- poj1026
- poj1026
- poj1026
- poj1026
- Android招式之美,学习笔记,开篇
- 译文 日经春秋 20150128
- 转自 三维数字地球发布平台探索--几款开源软件介绍
- Ubuntu安装VMware设置共享
- $.getJson中文乱码问题
- poj1026--Cipher
- linux下安装python运行环境
- Rayeager PX2 增强板关于GPU的选择
- mysql锁机制总结
- opencv read_csv
- ORA-12899: value too large for column
- centos关于禁用触摸板
- windows下安装JDK
- Linux 采用后台模式导入oracle数据库dmp文件