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 PP 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)的更多相关文章

  1. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  2. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  3. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  4. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

  5. ZOJ Problem Set - 1049 I Think I Need a Houseboat

    这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...

  6. ZOJ Problem Set - 1006 Do the Untwist

    今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...

  7. ZOJ Problem Set - 1001 A + B Problem

    ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...

  8. zoj 1788 Quad Trees

    zoj 1788 先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归 四分树建立完后, ...

  9. ZOJ 1958. Friends

    题目链接: ZOJ 1958. Friends 题目简介: (1)题目中的集合由 A-Z 的大写字母组成,例如 "{ABC}" 的字符串表示 A,B,C 组成的集合. (2)用运算 ...

随机推荐

  1. VC调试笔记

    1.windows-32调试: ①使用map文件根据崩溃地址寻找对应的源代码文件和行号 勾选project->settings->link->General mapfile,对应的P ...

  2. 涂抹Oracle笔记2:数据库的连接-启动-关闭

    一.数据库的连接sqlplus <username>[/<password>][@<connect_idertifier>]|/[as sysdba| as sys ...

  3. 【SQL学习笔记】排名开窗函数,聚合开窗函数(Over by)

    处理一些分组后,该组按照某列排序后 ,取其中某条完整数据的问题. 或 按照其中不同列分组后的聚合 比如 sum,avg之类. MSDN上语法: Ranking Window Functions < ...

  4. jquery 底部导航透明度变化

    如果滚动条到达底部,footer-nav 的透明度为1,否则为0.8: html <div class="footer-nav" id="footer"& ...

  5. 失效的URL访问限制(转)

    * 经常URL的保护仅仅是连接到该页面的链接不出现在未授权的用户面前.然而,一个有动机的.熟练的或者仅仅是幸运的黑客可能会找到并访问这些网页 , 调用这些功能并查看数据.在应用程序中,通过隐匿来实现安 ...

  6. ListHelper

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data; ...

  7. Cisco cmd命令(三)动态路由协议

    路由选择协议:1.矢量距离协议 2.链路状态协议 RIP路由选择协议:1.使用矢量距离协议 2.RIPv1只能使用有类路由 3.RIPv2可以使用无类路由 路由更新定时器:用于将路由器本身完整的路由选 ...

  8. (转载)Windows下手动完全卸载Oracle

    使用Oracle自带的Universal Installer卸载存在问题: 不干净,不完全,还有一些注册表残留,会影响到后来的安装. 所以,推荐使用手工卸载Oracle. 1.[win+R]-> ...

  9. 如何更改 Mac OS X 系统默认用户名

    说到 Mac 用户名估计有许多人都不知道在哪个地方修改,其实说简单也简单说麻烦也麻烦看你自己的需求.好比如果你只要用户名的登录更改,那是就非常简单的事了.下面这里就给大家介绍mac osx系统如何更改 ...

  10. Intel Core i7的整体操作

    Intel Core i7的整体操作(我们也称呼为Nehalem,他的项目代码名) 主要分成2个部分-指令控制单元Instruction Control Unit(ICU),负责从存储器读出指令序列, ...