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)用运算 ...
随机推荐
- Find Peak Element 解答
Question A peak element is an element that is greater than its neighbors. Given an input array where ...
- jquery ajax 局部table 刷新技术
点击查询:
- iOS面试知识点
1 iOS基础 1.1 父类实现深拷贝时,子类如何实现深度拷贝.父类没有实现深拷贝时,子类如何实现深度拷贝. 深拷贝同浅拷贝的区别:浅拷贝是指针拷贝,对一个对象进行浅拷贝,相当于对指向对象的指针进行复 ...
- 利用ant进行编译和发布项目
本文通过一个示例来解说如何通过ant进行编译和发布项目.本例按如下目录结构来组织项目. D:/web/antsample项目根目录 D:/web/antsample/src源代码目录 D:/web/a ...
- NPOI之使用EXCEL模板创建报表
因为项目中要用到服务器端创建EXCEL模板 无法直接调用EXCEL 查了下发现NPOI很方便很简单就实现了 其中走了点弯路 第一次弄的时候发现输出的值是文本不是数字型无法直接计算公式 然后又发现打开报 ...
- 把Go程序变小的办法
把Go程序变小的办法是: go build -ldflags “-s -w” (go install类似) -s去掉符号表(然后panic时候的stack trace就没有任何文件名/行号信息了, 这 ...
- 调起qq临时通话
1.在qq推广的网站上的推广工具中将个人或者公司的qq确认为推广qq 2. 选择好显示的图片,将代码拷贝到页面上就可以了
- CLR via C# - CLR模型
博客园对markdown支持不佳,错乱移步Github IO 博文 CLR 的执行模型 模块/程序集 1.模块 托管模块组成部分 PE32/PE32+头 : PE即Portable Executabl ...
- overflow:hidden
原来以为overflow:hidden能隐藏所有溢出的子元素,但今天发现我错了. 对于overflow:hidden的最大误解时:当一个具有高度和宽度中至少一项的容器应用了overflow:hidde ...
- Javascript - IE8下parseInt()方法的取值异常
公司的测试小妹妹跑来对我说,下拉框第9项始终无法正确提交的时候,我还以为见鬼了. parseInt()会把'0'开头的数字以8进制来解析,当有大于7的数字时候就按10进制来解析. // p ...