Description

“Be subtle! Be subtle! And use your spies for every kind of business. ”
― 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

There are several test cases.
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

For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is the desired answer.
代码(46MS):

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

  1. 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 ...

  2. 2012 Asia Chengdu Regional Contest

    Browsing History http://acm.hdu.edu.cn/showproblem.php?pid=4464 签到 #include<cstdio> #include&l ...

  3. 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) ...

  4. HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)

    Problem Description In this problem, you are given several strings that contain only digits from '0' ...

  5. HDU 4433 locker 2012 Asia Tianjin Regional Contest 减少国家DP

    意甲冠军:给定的长度可达1000数的顺序,图像password像锁.可以上下滑动,同时会0-9周期. 每个操作.最多三个数字连续操作.现在给出的起始序列和靶序列,获得操作的最小数量,从起始序列与靶序列 ...

  6. 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 ...

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

  8. 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 ...

  9. 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 ...

随机推荐

  1. vue 修改框架less变量

    以vant框架为例,vue项目以less作为css处理器: less/var-reset.less @import '~vant/lib/index.less'; // Color variables ...

  2. spring入门(五) spring mvc+hibernate

    核心是让SessionFactory由Spring管理 1.引入依赖 <!-- https://mvnrepository.com/artifact/org.springframework/sp ...

  3. Angularjs基础(四)

    AngularJS过滤器 过滤器可以使用一个管道符(|)添加到表达式和指令中. AngularJS过滤器可用于转换数据: currency 格式化数字为货币格式 filter 从数组中选着应子集. l ...

  4. setLocale的一个用处

    setLocale是C库中的一个设置地域化信息的C函数. 函数原型为: char *setlocale(int category, const char *locale) 参数解释: category ...

  5. jenkins部署记录

    环境规划 主机分配 192.168.2.139 : gitlab 192.168.2.141 : jenkins 192.168.2.142 : haproxy01 192.168.2.143 :ha ...

  6. 【mysql学习-1】

    part-1: #use mysql;/*show tables;select * from user;use mysql;show databases;#create database db1; # ...

  7. node 写api几个简单的问题

    最近出了一直在做无聊的管理后台,还抽空做了我公司的计费终端,前端vue,后端node,代码层面没啥太多的东西.由于自己node版本是8.0.0,node自身是不支持import和export的,要想基 ...

  8. YII2.0学习二 安装adminlte 后台模板

    控制台切换到安装目录wwwroot/shanghai/ 修改一下composer镜像地址:composer 使用中国镜像 运行 composer require dmstr/yii2-adminlte ...

  9. php Laravel5.5 表单验证常用的验证规则,以及示例

    namespace App\Http\Controllers; use App\Models\Users; use Illuminate\Support\Facades\Validator; use ...

  10. QP之QEP原理

    1.QP简介: 量子平台(Quantum Platform, 简称QP)是一个用于实时嵌入式系统的软件框架,QP是轻量级的.开源的.基于层次式状态机的.事件驱动的平台. QP包括事件处理器(QEP). ...