最长公共前缀后缀
Problem statement:
问题陈述:
Given a string of character, find the length of longest proper prefix which is also a proper suffix.
给定一个字符串,找到最长适当前缀的长度,这也是一个适当的后缀。
Input:
输入:
First line is T number of test cases. Each test case has one line denoting the string of length less than 100000.
第一行是T个测试用例。 每个测试用例都有一行表示长度小于100000的字符串。
Output:
输出:
Print length of longest proper prefix which is also a proper suffix.
打印最长适当前缀的长度,该长度也是适当的后缀。
Examples:
例子:
Input:
T = 1
"abcdabc"
Output:
3
//as prefix "abc" is the longest proper prefix
//present in the string.
Input:
T = 1
"abcdefghijklm"
Output:
0
//as there is no prefix which is a suffix.
Input:
T = 1
"aaaaaaa"
Output:
6
//as for proper prefix we can't have entire length
// as the prefix so only length possible is 6.
Solution Approach
解决方法
1) Brute Force Approach
1)蛮力法
Since the longest proper prefix which is also a suffix cannot be equal to the entire length of the string because it can't overlap each other, so we break the string from mid-point and start matching left and right string. If they are equal return size of any one string else try for shorter lengths on both sides.
由于最长的正确前缀(也是后缀)不能等于字符串的整个长度,因为它不能相互重叠,因此我们从中点处断开字符串,然后开始匹配左右字符串。 如果它们的返回值等于任一字符串的大小,则请尝试在两侧都使用较短的长度。
Pseudo Code:
伪代码:
void solve(string str)
{
// declare tw