ZOJ(3455)
Shizuka's Letter
Time Limit: 2 Seconds Memory Limit: 65536 KB
Nobita receives a letter from Shizuka. For the sake of security, the letter's content T is produced as follows, first the message is written, then the substitution cipher and transposition cipher is applied to the message. Finally, the encrypted message is inserted into another text.
Substitution cipher changes all occurrences of each letter to some other letter. Substitutes for all letters must be different. For example, after substitution cipher, the message "BBA" can become "AAC".
Transposition cipher applies some permutation to the letters of the message. For example, after applying the permutation (1, 3, 2), the second letter moves to the third place, the third letter moves to the second place, so the message "AAC" turns to "ACA".
For example, suppose the original message is "BBA", after substitution, it becomes "AAC", then after transposition, it becomes "ACA". Finally the encrypted message "ACA" is inserted into another text "CBC", after letter 'B', so the content of the letter is "CBACAC".
Nobita don't know what the original message is, but he guesses the message should be P. Now given the letter's content T and Nobita's guess P, you should determine if Nobita's guess is possible to be right.
Input
There are multiple cases.
For each case, the first line is letter's content T, the second line is Nobita's guess P. P and T are strings consist of characters whose ASCII code is between 33 ('!') and 126 ('~'). The length of P and T is no more than 500000. P is guaranteed to be shorter than T.
Output
For each case, if Nobita's guess is possible to be right, output "Yes", otherwise output "No". If the answer is "Yes", it should be followed by another line of integers which denote all the positions (0-based) of the message in the content.
Sample Input
CBACAC
BBA
ABCDE
BBA
Sample Output
Yes
2 3
No
转:http://blog.watashi.ws/1760/zojmonthly1012/
题意:有一种加密方法,对于字符串P,首先将字符串中的字符进行替换,然后可以对位置进行交换,最后将它插入另一个字符串中间获得加密串T。然后对于一对T与P,求T是否可以为P的一个加密结果,若是则求所有的加密位置。
思路:首先只考虑替换和交换这两步,那么会知道,这样变化后会满足出现i次的字母的个数l[i]是不变的,反之,如果满足这个条件,则一定可以通过替换加交换得到。而从位置j到位置j+1,多了一个字符T[j+n],少了T[j],所以最多有两个l[i]发生了变化。如果我们用一个变量k,记录有多少l[i]与P的一致,那么k==SIGMA的时候,就是一个合法的位置。每一步,这些变量都可以在O(1)的时间内得到维护,所以总的复杂度是O(n)的。
难点:维护k。
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 500500
#define ASCII 256
int c[ASCII];
int l[maxn];
char T[maxn];
char P[maxn];
int main()
{
int n,m,k;
vector<int>ans;
while(~scanf("%s%s",T,P))
{
n=strlen(T);
m=strlen(P);
fill(c,c+ASCII,);
for(int i=;i<m;i++)//字符作下标对应的是是ASCII码。
c[P[i]]++;
fill(l+,l++m,);
for(int i=;i<ASCII;i++)
l[c[i]]--;
k=count(l+,l++m,); ans.clear();
fill(c,c+ASCII,);
for(int i=;i<n;i++)
{
if(i>=m)
{
int &temp1=c[T[i-m]];//不能掉了引用符号
if(l[temp1]==)
k--;
if(l[temp1]==)
k++;
l[temp1]--;
temp1--;
if(temp1!=)
{
if(l[temp1]==-)
k++;
if(l[temp1]==)
k--;
l[temp1]++;
}
}
int &temp2=c[T[i]];
if(temp2!=)
{
if(l[temp2]==)
k--;
if(l[temp2]==)
k++;
l[temp2]--;
}
temp2++;
if(l[temp2]==-)
k++;
if(l[temp2]==)
k--;
l[temp2]++;
if(k==m)
{
ans.push_back(i-m+);
}
}
if(ans.empty())
puts("No");
else
{
puts("Yes");
for(int i=;i<ans.size();i++)
printf(i==ans.size()-?"%d\n":"%d ",ans[i]);
}
}
return ;
}
ZOJ(3455)的更多相关文章
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
- ZOJ Problem Set - 1049 I Think I Need a Houseboat
这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...
- ZOJ Problem Set - 1006 Do the Untwist
今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...
- ZOJ Problem Set - 1001 A + B Problem
ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...
- zoj 1788 Quad Trees
zoj 1788 先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归 四分树建立完后, ...
- ZOJ 1958. Friends
题目链接: ZOJ 1958. Friends 题目简介: (1)题目中的集合由 A-Z 的大写字母组成,例如 "{ABC}" 的字符串表示 A,B,C 组成的集合. (2)用运算 ...
随机推荐
- 简述tcp协议对http性能的影响及优化
当网站服务器并发连接达到一定程度时,你可能需要考虑服务器系统中tcp协议设置对http服务器的影响. tcp相关延时主要包括: 1.tcp连接时建立握手: 2.tcp慢启动拥塞控制: 3.数据聚集的N ...
- IT技术如何转向销售创业
广州 IT 网友 14:14:10 本人本科软件工程毕业,现在在一家公司做开发(不是很技术的那种),工作接近两年感觉自 己不适合这个行业,想换销售行业,不愿意在做技术.打算从事本行业的销售,老师给点 ...
- 第17讲- UI常用组件之ImageView图片浏览
第17讲 UI常用组件之ImageView图片浏览 二.图片浏览ImageView ImageView就是一个用来显示图片的视图: ImageView常见属性 常见属性 对应方法 说明 android ...
- python之路-模块 splinter
Splinter介绍 Splinter is an open source tool for testing web applications using Python. It lets you au ...
- Subsequence(两个单调队列)
Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- Visual Studio 2015 使用ODP.net进行EF开发
刚转了新公司,以前公司都是用VS+MSSQL作为开发工具的 现在新公司由于数据库是Oracle,而且新公司比较小规模,开发团队也没有什么规范 访问数据库的方式一直使用ADO.net的DataTable ...
- Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.3
Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.3 http://blog.csdn.net/sunbow0 第二章Deep ...
- Git 如何回到过去,然后 再 回到将来
回到过去: git log 然后 git reset --hard commit ID (那段长长代码 40位) 再,回到将来git reflog 然后 git reset --hard 前面那个代码 ...
- 伪元素first-letter(首字母变大)
让首字母变大 <p>Do you like to ride a bicycle?</p> p:first-letter{ font-size: 34px; }
- css学习之color: window和color: currentColor
一.易被忽略的属性 color: currentColor color: window 看完之后感觉眼前一亮,有的我之前根本没有用过,甚至都不知道有color: currentColor这么个东西 ...