HDU 4468 Spy(KMP+贪心)(2012 Asia Chengdu Regional Contest)
Description
― Sun Tzu
“A spy with insufficient ability really sucks”
― An anonymous general who lost the war
You, a general, following Sun Tzu’s instruction, make heavy use of spies and agents to gain information secretly in order to win the war (and return home to get married, what a flag you set up). However, the so-called “secret message” brought back by your spy, is in fact encrypted, forcing yourself into making deep study of message encryption employed by your enemy.
Finally you found how your enemy encrypts message. The original message, namely s, consists of lowercase Latin alphabets. Then the following steps would be taken:
* Step 1: Let r = s
* Step 2: Remove r’s suffix (may be empty) whose length is less than length of s and append s to r. More precisely, firstly donate r[1...n], s[1...m], then an integer i is chosen, satisfying i ≤ n, n - i < m, and we make our new r = r[1...i] + s[1...m]. This step might be taken for several times or not be taken at all.
What your spy brought back is the encrypted message r, you should solve for the minimal possible length of s (which is enough for your tactical actions).
Input
For each test case there is a single line containing only one string r (The length of r does not exceed 105). You may assume that the input contains no more than 2 × 106 characters.
Input is terminated by EOF.
Output
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXN = ; char r[MAXN], s[MAXN];
int fail[MAXN];
int n; int main() {
int test = ;
while(scanf("%s", r) != EOF) {
int n = strlen(r), m = , last = ;
memset(s, , sizeof(s));
s[] = r[];
for(int i = , j = ; i < n; ++i) {
while(j && r[i] != s[j]) j = fail[j];
if(r[i] == s[j]) ++j;
if(!j) {
for(int k = last; k <= i; ++k) {
s[m] = r[k];
int t = fail[m];
while(t && s[m] != s[t]) t = fail[t];
fail[m + ] = t + (s[m] == s[t]);
++m;
}
j = m;
}
if(j == m) last = i + ;
}
printf("Case %d: %d\n", ++test, m + n - last);
}
}
HDU 4468 Spy(KMP+贪心)(2012 Asia Chengdu Regional Contest)的更多相关文章
- HDU 4467 Graph(图论+暴力)(2012 Asia Chengdu Regional Contest)
Description P. T. Tigris is a student currently studying graph theory. One day, when he was studying ...
- 2012 Asia Chengdu Regional Contest
Browsing History http://acm.hdu.edu.cn/showproblem.php?pid=4464 签到 #include<cstdio> #include&l ...
- HDU-4432-Sum of divisors ( 2012 Asia Tianjin Regional Contest )
Sum of divisors Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)
Problem Description In this problem, you are given several strings that contain only digits from '0' ...
- HDU 4433 locker 2012 Asia Tianjin Regional Contest 减少国家DP
意甲冠军:给定的长度可达1000数的顺序,图像password像锁.可以上下滑动,同时会0-9周期. 每个操作.最多三个数字连续操作.现在给出的起始序列和靶序列,获得操作的最小数量,从起始序列与靶序列 ...
- HDU 4115 Eliminate the Conflict(2-SAT)(2011 Asia ChengDu Regional Contest)
Problem Description Conflicts are everywhere in the world, from the young to the elderly, from famil ...
- HDU 4441 Queue Sequence(优先队列+Treap树)(2012 Asia Tianjin Regional Contest)
Problem Description There's a queue obeying the first in first out rule. Each time you can either pu ...
- HDU 4433 locker(DP)(2012 Asia Tianjin Regional Contest)
Problem Description A password locker with N digits, each digit can be rotated to 0-9 circularly.You ...
- HDU 4431 Mahjong(枚举+模拟)(2012 Asia Tianjin Regional Contest)
Problem Description Japanese Mahjong is a four-player game. The game needs four people to sit around ...
随机推荐
- Angularjs 数据双向绑定
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- generative models
A generative model G can be seen as taking a random seed h (say, a sample from a multivariate Normal ...
- iOS之UITextField输入错误的震动动画
//左右震动效果 - (void)shake:(UIView *)view { CGRect frame = view.frame; CAKeyframeAnimation *shakeAnimati ...
- c# 本地完整缓存组件
用了一段时间java,java实现服务端程序很简单,有很多公共开源的组件或者软件.但是c#的很少. 现在准备自己写点东西,学习下新的东西,总结下c#的内容以及我们经常用的内容,抽离成类,组件,模型.方 ...
- MySQL Waiting for table metadata lock的解决方法
最近需要在某一个表中新增字段,使用Sequel Pro 或者Navicat工具都会出现程序没有反应,使用 show processlist 查看,满屏都是 Waiting for table meta ...
- SQL语句笔记/好记性不如烂笔头/持续更新
常用的增删改查操作,针对库,表,字段,记录分类有助于记忆,当然熟能生巧,还是需要多多实操 库操作 删除库 drop database dbx; 列出所有库 show databases; 切换库 us ...
- python3 练习题100例 (二十五)打印一个n层金字塔
题目内容: 打印一个n层(1<n<20)金字塔,金字塔由“+”构成,塔尖是1个“+”,下一层是3个“+”,居中排列,以此类推. 注意:每一行的+号之后均无空格,最后一行没有空格. 输入格式 ...
- ssh 远程命令
远程拷贝文件,scp -r 的常用方法: 1.使用该命令的前提条件要求目标主机已经成功安装openssh-server 如没有安装使用 sudo apt-get install openssh-ser ...
- Failed to read candidate component class错误分析
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component c ...
- 42-EF Core Migration
1-常用命令 1-由于2.1版本有点不一样,不会自动创建ApplicationUser类,发现合并没效果.暂时略 增加一个字段 E:\coding\netcore\IdentitySample> ...