hdu4468 Spy kmp

2024-05-14 12:38
文章标签 kmp spy hdu4468

本文主要是介绍hdu4468 Spy kmp,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 227    Accepted Submission(s): 107


Problem 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 10 5). You may assume that the input contains no more than 2 × 10 6 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.

Sample Input
  
abc aab abcadabcabcad aaabbbaaaabbbaa abcababcd

Sample Output
  
Case 1: 3 Case 2: 2 Case 3: 5 Case 4: 6 Case 5: 4

Source
2012 Asia Chengdu Regional Contest

Recommend
liuyiding
这题,我们可以很容易看出也是kmp,那么怎么做呢?我们就是要去模式串满足其前缀可以拼成原串,且是原串的后缀,我们可以先不管后缀,我们先找前缀满足,我们先为原串的第一个字符,然后,我们,与原串匹配,如果,发现不能匹配,就把上一次完全匹配的地方到这里的全加入模式串,一直到最后,不就得出答案了么?如果到了最后,不是反缀,就可以,再加入新的,否则直接输出就可以了!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 100000
char str[MAXN],pass[MAXN];
int next[MAXN],passnum,strnum,lastpass;
void getnext()
{int i,j;next[0]=next[1]=0;for(i=lastpass,j=0;i<passnum;i++){j=next[i];while(j&&pass[i]!=pass[j]){j=next[j];}next[i+1]=pass[i]==pass[j]?j+1:0;}}
int fkmp()
{int i,j,last,k;last=1;for(i=1,j=0;i<strnum;i++)//??{while(j&&str[i]!=pass[j]){j=next[j];}if(str[i]==pass[j]){j++;}if(j==0){lastpass=passnum;for(k=last;k<=i;k++){pass[passnum++]=str[k];}last=i+1;pass[passnum]='\0';getnext();//printf("%s %d %d %d\n",pass,i,j,passnum);}if(j==passnum){last=i+1;j=0;//完全}}if(last==strnum)return passnum;elsereturn strnum-last+passnum;
}
int main()
{int tcase=1;while(scanf("%s",str)!=EOF){strnum=strlen(str);lastpass=1;passnum=1;pass[0]=str[0];pass[1]='\0';printf("Case %d: %d\n",tcase++,fkmp());}return 0;
}

这篇关于hdu4468 Spy kmp的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/988766

相关文章

codeforces535D:Tavas and Malekas(KMP)

(i-1 , i)有重合的时候 ,从第i位开始的子串必须是模式串的前缀。 而同时,从第i位开始的子串本来就已经是模式串的后缀了。 typedef long long LL ;const int maxn = 1000008 ;int next[maxn] ;void getnext(char s[]){int len = strlen(s) ;next[0] = -1 ;i

fzu 2275 Game KMP

Problem 2275 Game Time Limit: 1000 mSec    Memory Limit : 262144 KB  Problem Description Alice and Bob is playing a game. Each of them has a number. Alice’s number is A, and Bob’s number i

VS2022如何安装Spy+

Microsoft Spy++是一个非常好的查看Windows操作系统的窗口、消息、进程、线程信息的工具,简单易用,功能强大。 step1 安装vc++,点击下面官方链接进行下载 官方链接-VS++  下载完成后运行 点击继续 ,等待安装    等待安完成,自动启动  step2 暂时跳过此项  选择无需代码 选择工具,获取工具和功

KMP 详解

KMP数组存的是什么 对于一个字符串 b,下标从1开始。 则kmp[i]表示 以i结尾的连续子串 = s的前缀的最大值(等价于前缀最大结尾处) 如何求KMP 假设 i 以前的KMP都被求出来了。 j 表示上一个字符可以成功匹配的长度(等价于下标) 如果b[j+1] != b[i]下一个位置匹配不上(即不能成为前缀) 则,让j = kmp[j] 即成为以j结尾的 连续子串 的 最长前缀

KMP算法详解 --- 彻头彻尾理解KMP算法

前言     之前对kmp算法虽然了解它的原理,即求出P0···Pi的最大相同前后缀长度k。   但是问题在于如何求出这个最大前后缀长度呢?   我觉得网上很多帖子都说的不是很清楚,总感觉没有把那层纸戳破,   后来翻看算法导论32章 字符串匹配,虽然讲到了对前后缀计算的正确性,但是大量的推理证明不大好理解,没有与程序结合起来讲。   今天我在这里讲一讲我的一些理解,希望大家多多指教

扩展KMP --- HDU 3613 Best Reward

Best Reward Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3613   Mean:  给你一个字符串,每个字符都有一个权值(可能为负),你需要将这个字符串分成两个子串,使得这两个子串的价值之和最大。一个子串价值的计算方法:如果这个子串是回文串,那么价值就是这个子串所有字符权值之和;否则价值为0。

数据结构串的实现以及KMP改进算法

</pre><pre name="code" class="cpp">#include <stdio.h>#include <stdlib.h>#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define MAXSIZE 40 /* 存储空间初始分配量 */typedef int Status; /* Status是

字符串匹配算法之KMP算法和BM算法

[尊重原创]-原文链接在这里->http://blogread.cn/it/article/3975?f=wb 本文主要介绍KMP算法和BM算法,它们分别是前缀匹配和后缀匹配的经典算法。所谓前缀匹配是指:模式串和母串的比较从左到右,模式串的移动也是从左到右;所谓后缀匹配是指:模式串和母串的的比较从右到左,模式串的移动从左到右。看得出来前缀匹配和后缀匹配的区别就仅仅在于比较的顺序不同。下文分别

KMP算法 KMP模式匹配 二(串)

B - KMP模式匹配 二(串) Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Description 输入一个主串和一个子串,用KMP进行匹配,问进行几趟匹配才成功,若没成功,则输出0

HDU 1358(kmp)

题意:给出一个数字n,接下来一行是一个字符串,n是这个字符串的长度。求这个字符串的所有是循环字符串的前缀。 kmp中的next数组只得是第i个字符匹配错误,向前跳的位置next【i】。   #include<algorithm>#include<stdio.h>#include<string>#include<string.h>#include<math.h>#include<qu