最长公共前缀后缀_最长的前缀和后缀

最长公共前缀后缀

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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值