UVa 306 / POJ 1026 / Central Europe 1995 Cipher (置换群)

POJ测试数据 1、USACO 2006年November题目和测试数据的网址 http://ace.delos.com/NOV06 2007年open赛题目和测试数据的网址 http://ace.delos.com/OPEN07 以此类推 2、日本ACM比赛 http://www.acm-japan.org/ http://icpc2010.honiden.nii.ac.jp/en/past-c 阅读详情

306 - Cipher

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=242

http://poj.org/problem?id=1026

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, tex2html_wrap_inline27 , greater than zero and less or equal ton. 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 positioni is written in the encoded message at the position tex2html_wrap_inline33 , wheretex2html_wrap_inline33 is the corresponding number in the key. And then the encoded message is encoded in the same way. This process is repeatedk times. Afterkth encoding they exchange their message.

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

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

Input

The input file consists of several blocks. Each block has a number tex2html_wrap_inline51 in the first line. The next line contains a sequence ofn numbers pairwise distinct and each greater than zero and less or equal thann. Next lines contain integer numberk 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

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 lenghtn. After each block there is one empty line.

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     


“Character in the message at the position i is written in the encoded message at the positiontex2html_wrap_inline33 , wheretex2html_wrap_inline33 is the corresponding number in the key.”

加密过程如下:(以原文的第7个字符ch为例)

在样例中,ch被写在一次加密的密文中的第a7处(即第1个位置),进而可推知ch被写在二次加密的密文中的第a(a7)处(即第4个位置),三次加密的密文中的第a(a(a7))处(即第7个位置)。简记为a^1(7)=1,a^2(7)=4,a^3(7)=7。

若把a^0(7)=7也算在内,我们似乎发现了一些周期性的规律:a^(m%3)(7)=7,1,4 (m%3=0,1,2)

据此,把序列{an}写成不交轮换的合成的形式:(4 7 1)(5 2)(3)(8 6)(10 9) 

这样,原文的第i个字符在k次加密的密文中,应被写在第a^(k%Ti)(i)处,其中Ti指i所在的轮换的长度(也可以称作最小正周期),具体的计算细节见下方代码。


注意:不要用"%d "读掉中间空格,因为可能原文的第一个字符就是空格。


复杂度:O(n)

完整代码:

/*UVa: 0.049s*/
/*POJ: 94ms,180KB*/

#include<cstdio>
#include<cstring>
#include<vector>
#include<utility>
using namespace std;
const int maxn = 205;

char s[maxn], ans[maxn];
int a[maxn];
bool vis[maxn];
pair<int, int> belong[maxn];
vector<int> group[maxn], tmp;

int main()
{
	int n, i, j, cnt, k;
	while (scanf("%d", &n), n)
	{
		for (i = 1; i <= n; ++i)
		{
			scanf("%d", &k);
			a[i] = k;
		}
		memset(vis, 0, sizeof(vis));
		for (i = 0; i < n; ++i) group[i].clear();
		cnt = 0;
		for (i = 1; i <= n; ++i)
		{
			if (!vis[i])
			{
				for (j = a[i]; !vis[j]; j = a[j])
				{
					belong[j] = make_pair(cnt, group[cnt].size());
					group[cnt].push_back(j);///构造轮换
					vis[j] = true;
				}
				++cnt;
			}
		}
		///prepare
		while (scanf("%d", &k), k)
		{
			getchar();///为什么上面不写"%d "来读掉中间的空格?因为可能原文的第一个字符就是空格orz...
			gets(s + 1);
			for (i = strlen(s + 1) + 1; i <= n; ++i)
				s[i] = ' ';
			for (i = 1; i <= n; ++i)
			{
				tmp = group[belong[i].first];
				ans[tmp[(belong[i].second + k) % tmp.size()]] = s[i];
			}
			ans[n + 1] = 0;
			puts(ans + 1);
		}
		putchar(10);
	}
	return 0;
}

『杭电1439』 Cipher Problem Description 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 Fe 阅读详情

相关推荐

Southwest Europe 1996测试数据

Southwest Europe 1996测试数据

[ACM] POJ 1026 Cipher (组合数学,置换)

Cipher Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19228   Accepted: 5148 Description Bob and Alice started to use a brand-new encoding scheme. Surpris

逆风的方向 更适合飞翔 2034

实验六 存储器实验

只读存储器(ROM)是一种在正常工作时其存储的数据固定不变,其中的数据只能读出,不能写入,即使断电也能够保留数据,要想在只读存储器中存入或改变数据,必须具备特定的条件。当地址为00000101时,会分别在数据位宽为8,16,32的只读寄存器中找到地址为00000101所对应的十进制地址位置,也就是地址为5的数据,并将其分别以8位,16位,32位输出。因此,输出保持为0。因此,当异步清零信号为1时,无论其他输入信号是什么,触发器的输出都会被强制清零,输出为0。1)寄存器输入0011,使能端1,异步清零0。

wenmou1111的博客 3005

poj1026

Cipher Description 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 cho

◆◇丶生如夏花。谁来订阅我的悲伤 535

麒麟V10(arm)配置本地离线yum源

麒麟V10(arm)配置本地离线yum源

myfm2java的博客 7889

佳能C3025打印机扫描方法

佳能C3025彩色打印机扫描方法 步骤一、如果打印机处于休眠状态,可按图中标记的按钮唤醒打印机; 然后点击屏幕上的【扫描】 步骤二、扫描类型选择【文件】 步骤三、点击屏幕上的【地址簿】 步骤四、此时屏幕胡看到名字,如果看不到自己的名字,可往下滑动直到看到自己的名字,然后点击自己的名字,再点击右下角的【应用】 步骤五、点击【发送设置】,可看到文件格式为PDF,点击可以选择JPEG的图片格式。...

weixin_46019317的博客 2万+

Sticks(Central Europe 1995)

Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sti

每一次AC都是一种感动 1459

PlayReady Encrypt XAP 解密讨论

目前WP7市场上的绝大部分XAP都被MS进行了加密,经过分析,这个加密方式采用了AES方式进行了加密,本人经过对新的XAP格式(PRE)进行了分析,发现新的格式里,实际上是对之前PK(ZIP格式)的XAP进行了加密然后封装:   typedef struct {   DWORD HDR; // 0x07455250 : PRE & 0x7   DWORD a1; // always 0x1   D

u013729125的专栏 1510

1026 Cipher

Cipher Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5345   Accepted:

xcl119xcl的专栏 854

从轴承润滑到代码实现:手把手教你用Python FDM求解稳态雷诺方程

本文详细介绍了如何用Python FDM(有限差分法)求解稳态雷诺方程,从轴承润滑理论到代码实现的全过程。通过构建无量纲方程、有限差分格式和迭代求解算法,开发者可以快速实现专业级润滑分析,应用于涡轮发电机、汽车曲轴等场景。文章还提供了完整的Python代码示例和结果可视化方法,帮助工程师优化轴承设计。

weixin_28689507的博客 119

树莓派 RP2350B 开发环境搭建

本文介绍了树莓派RP2350B开发环境的搭建过程。

张一西的博客 287

Cipher(P1026)

好几天没有做题了,现在做一个,,感觉就好吃力的样子。 这个题首先直接模拟是会严重超时的。 先算所有的字符循环多少次后可以加原来的位置。由于,计算得到的结果中字符所对应的位是唯一确定的。所以就计算一下求出的模的次数就行(注意:用最小公倍数会超时,可能是整数存不了这么大的数了) #include #include #include using namespace

jiangjiashi的博客 529

DeepSeek Harness 工程实践与高级特性:从测试到安全的完整指南

AI Agent 能执行命令、读写文件、自主决策,能力越强风险越大——删文件、泄密钥、死循环、竞态条件随时可能发生。本文是 DeepSeek Harness 系列第三篇,深入剖析其工程实践与高级特性:从单元测试到真实 API 冒烟测试的分层策略,拒绝 Mock 依赖、坚持"验证世界而非自我报告";从正交结果、规范化契约到 Dispose 静止的防御式编程铁律;以及跨平台多层进程沙箱、文件系统版本守卫、fail-closed 审批机制、模型编写的工作流引擎与上下文压缩等高级能力。

program哲学 207

POJ 1026 Cipher(置换+循环节)

Cipher Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22366   Accepted: 6187 Description Bob and Alice started to use a brand-new encoding scheme. Surprisin

信仰.的博客 594

Vue - 解决报错 Error: Cannot find module ‘XXX\@vue\cli-service\bin\vue-cli-service.js‘(vue项目编译运行出错,启动失败)

vue报错,Error: Cannot find module ‘@vue\cli-service\bin\vue-cli-service.js‘,vue2项目启动报错,vue3项目运行出错,vue执行npm run serve报错,找不到@vue/cli-service模块,vue项目关于:Error: Cannot find module ‘D:\@vue\cli-service\bin\vue-cli-service.js‘的解决方案,vue项目跑不起来了,Vue项目报错,跑项目报错Cannot fi

🏆 前端领域优质创作者 3115

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

acm.jlu.edu.cn-1068-Cipher

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

Life, it seems, will fade away 1150

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

poj 1026 加密问题

如何改变文本的样式   题目大意是,给定一串密匙,求源码经过密匙加密k次后的内容。加密的规则是:源码第i个字母调换到密匙中ai指定的位置。同时,源码长度小于等于密匙长度,当源码长度小于密匙长度时,在源码后使用空格填充保证二者长度相等。   最开始使用的直接模拟加密过程的方法,会超时。明显要考虑加密过程中加密的内容周期问题,于是直接求了整个源码字符串的周期,这样也会超时。实际上,密匙长度在题目中限制为最大200,直接求整个字符串的周期,这个值也可能很大。参考了其它同学的思路,分开求每个字符的周期,然后对每个字

qq_40979892的博客 223

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

HDOJ 题目1439Cipher(置换群)

 Cipher Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 386    Accepted Submission(s): 171 Problem Description Bob and Alice started

Jogging_Clown 579

DFS深度搜索算法实现深度探究解析-以POJ 1040为例

POJ 1040 Transportation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2872   Accepted: 1176 Description Ruratania is just entering c

Coding for Dreams 2696

Cipher

Description 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 cho...

weixin_33726313的博客 201

Central Europe 1996 / UVa 311 / POJ 1017 Packets (贪心)

311 - Packets Time limit: 3.000 seconds  http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=247 A factory produces products packed in sq

AC,∑ndless 1445

POJ 1026 Cipher(置换)

Cipher Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19502   Accepted: 5239 Description Bob and Alice started to use a brand-new encoding scheme.

LYHVOYAGE的专栏 2023

poj 1026解题报告

<br /> CipherTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 13633 Accepted: 3438<br />DescriptionBob 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

neoxuhaotian的专栏 1470

POJ 1026 Cipher

题目链接 : POJ1026 Cipher Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17849   Accepted: 4712 Description Bob and Alice started to use a brand-new encoding sch

Salvare219 577

poj 1026 Cipher 【置换群】 【求一个字符串经过 k 次置换后 的新字符串】

Cipher Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20026   Accepted: 5376 Description Bob and Alice started to use a brand-new encoding scheme. Surprisingl

世界很大 1026

ACM: polya模拟 数论题 poj 1026

Cipher Description   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

smile_benson 428
上一篇: LightOJ 1136 Division by 3 (想法题)
下一篇: 【ACM训练计划】POJ 题目分类 【初级、中级、高级】
synapse7
博客等级 码龄14年 432粉丝 701原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值