HUST 1010-The Minimum Length-KMP
Description
There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me
the length of the shortest possible string A. For example, A="abcdefg". I got abcdefgabcdefgabcdefgabcdefg.... Then I cut the red part: efgabcdefgabcde as string B. From B, you should find out the shortest A.
Input
Multiply Test Cases. For each line there is a string B which contains only lowercase and uppercase charactors. The length of B is no more than 1,000,000.
Output
For each line, output an integer, as described above.
Sample Input
bcabcab efgabcdefgabcde
Sample Output
3 7
有一個字串,長度小於1000000。我一遍又一遍的重寫。然後我有了一個新的字串:aaaaaa ......現在我把它從兩個不同的位置,得到一個新的字串,然後,給你的字串B,你能告訴我,例如最短的字串的長度,A="abcdefg". I got abcdefgabcdefgabcdefgabcdefg....…然後我把紅色部分:efgabcdefgabcde作為字串B,你應該找出最短的A.
多試驗例。每一行有一個字串B只包含小寫和大寫。長度不超過1000000。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxt=1000000+10;
char t[maxt];
int next[maxt];
int main()
{
while(scanf("%s",t)!=EOF)
{
int p=0,cur,l;
next[0]=-1;
next[1]=0;
l=strlen(t);
for(cur=2; cur<=l; ++cur)
{
while(p>=0&&t[p]!=t[cur-1])
p=next[p];
next[cur]=++p;
}
printf("%d\n",l-next[l]);
}
return 0;
}
KMP模板題,呼叫求子模板串的模式值next[n],再用串的長度減去next[n]即得到答案。
相關文章
- B - Minimum Sum
- Range Minimum Sum
- hust 密碼學課設 rsa引數d密碼學
- 【計算幾何】Triangles HUST 1607
- HUST-計算機網路實驗-socket程式設計計算機網路程式設計
- Leetcode Minimum Path SumLeetCode
- LintCode-Minimum Subarray
- LintCode-Minimum Path Sum
- Leetcode-Minimum Path SumLeetCode
- [LeetCode] Minimum Size Subarray SumLeetCode
- Leetcode Minimum Window SubstringLeetCode
- Minimum Path Sum leetcode javaLeetCodeJava
- ABC 368D Minimum Steiner Tree
- [LeetCode] 727. Minimum Window SubsequenceLeetCode
- LeetCode-Minimum Size Subarray SumLeetCode
- LeetCode-Minimum Height TreesLeetCode
- Leetcode Minimum Depth of Binary TreeLeetCode
- LintCode-Minimum Adjustment Cost
- Leetcode-Minimum Window SubstringLeetCode
- Minimum Window Substring leetcode javaLeetCodeJava
- [USACO23DEC] Minimum Longest Trip G3D
- 64 - Minimum Path Sum 最小路徑和
- The minimum required version for Powerlevel10k is 5.1UI
- 【KMP思想求迴圈節】hdu 1358 hust 1010 poj 2406KMP
- 網易2007Hust遊戲開發工程師筆試遊戲開發工程師筆試
- Find Minimum in Rotated Sorted Array leetcode javaLeetCodeJava
- Leetcode-Minimum Depth of Binary TreeLeetCode
- Leetcode-Find Minimum in Rotated Sorted ArrayLeetCode
- Minimum Depth of Binary Tree leetcode javaLeetCodeJava
- [每日一題]452. Minimum Number of Arrows to Burst Balloons每日一題
- [LeetCode] 857. Minimum Cost to Hire K WorkersLeetCode
- rancher 的 deployment does not have minimum availability 問題AI
- 【Leetcode】453. Minimum Moves to Equal Array ElementsLeetCode
- LeetCode のminimum-depth-of-binary-treeLeetCode
- hdu 1394 Minimum Inversion Number 【線段樹查詢】
- Leetcode 453. Minimum Moves to Equal Array ElementsLeetCode
- Leetcode-Find Minimum in Rotated Sorted Array IILeetCode
- Leetcode 209. Minimum Size Subarray SumLeetCode