You're Given a String... (kmp)

本文介绍了一种通过KMP算法来解决寻找字符串中最长重复子串的问题。文章详细阐述了如何利用KMP算法进行子串匹配的过程,并给出了完整的C++实现代码。读者将学会如何通过枚举子串并使用KMP算法高效地找出至少出现两次的最长子串。

You're Given a String...


You're given a string of lower-case Latin letters. Your task is to find the length of its longest substring that can be met in the string at least twice. These occurrences can overlap (see sample test 2).

Input

The first input line contains the string. It's guaranteed, that the string is non-empty, consists of lower-case Latin letters, and its length doesn't exceed 100.

Output

Output one number — length of the longest substring that can be met in the string at least twice.

Example
Input
abcd
Output
0
Input
ababa
Output
3
Input
zzz
Output
2

枚举子串,kmp匹配

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
const int MAXN = 110;
string s;
string w;
int Next[MAXN];
void getNext(){
    int i = -1,j = 0;
    int len = s.length();
    memset(Next,0,sizeof(Next));
    Next[0] = -1;
    while(j < len){
        if(i == -1 || w[i] == w[j]){
            i++,j++;
            Next[j] = i;
        }
        else
            i = Next[i];
    }
}
int kmp(){
    int i = 0, j = 0,cnt = 0;
    int lens = s.length();
    int lenw = w.length();
    getNext();
    while(j < lens){
        if(i == -1 || w[i] == s[j])
            i++,j++;
        else
            i = Next[i];
        if(i == lenw)
            cnt++;
    }
    return cnt;
}
int main(){
    int i,j;
    int ans = 0;
    cin >> s;
    for(i = 0; i < s.length(); i++){
        for(j = i; j < s.length(); j++){
            w = s.substr(i,j-i+1);
            if(kmp() >= 2){
                int len = j-i+1;
                ans = max(ans,len);
            }
        }
    }
    cout << ans << endl;
    return 0;
}

`string.Contains` 方法用于判断一个字符串是否包含另一个指定的子字符串。它的内部实现通常依赖于高效字符串搜索算法,例如 Knuth-Morris-Pratt (KMP) 算法或 Boyer-Moore 算法。以下是 `string.Contains` 方法的一些关键点: 1. **时间复杂度**:`string.Contains` 方法的时间复杂度通常是 O(n),其中 n 是主字符串的长度。这是因为它需要遍历主字符串一次来查找子字符串。 2. **返回值**:该方法返回一个布尔值。如果主字符串包含子字符串,则返回 `true`;否则返回 `false`。 3. **大小写敏感性**:默认情况下,`string.Contains` 方法是大小写敏感的。如果需要进行不区分大小写的查找,可以将主字符串和子字符串都转换为同一大小写形式(例如,都转换为小写)后再进行比较。 4. **空字符串处理**:如果子字符串为空字符串,`string.Contains` 方法通常返回 `true`,因为空字符串被认为是任何字符串的子字符串。 以下是一些示例代码,展示了如何使用 `string.Contains` 方法: ```csharp using System; class Program { static void Main() { string mainString = "Hello, World!"; string substring1 = "World"; string substring2 = "world"; string emptyString = ""; bool contains1 = mainString.Contains(substring1); // true bool contains2 = mainString.Contains(substring2); // false bool contains3 = mainString.Contains(emptyString); // true Console.WriteLine(contains1); // 输出: True Console.WriteLine(contains2); // 输出: False Console.WriteLine(contains3); // 输出: True // 不区分大小写的查找 bool contains4 = mainString.IndexOf(substring2, StringComparison.OrdinalIgnoreCase) >= 0; // true Console.WriteLine(contains4); // 输出: True } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值