You're Given a String... CodeForces - 23A

本文介绍了一道CodeForces上的题目,要求找出字符串中最长的重复子串长度。通过四层循环暴力求解的方式,最终实现了正确的解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You're Given a String...

CodeForces - 23A

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
一道水题,让我wa了五遍才过,脑子思路不清楚,直接暴力,四个循环嵌套,最外面两个选择子串,里面两个从头开始一个一个对比子串 code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
int main(){
    string s;
    cin >> s;
    int i,j,k;
    int longest = 0;
    for(i = 0; i < s.length(); i++){
        for(j = i; j < s.length(); j++){//选择出子串i到j
            int index = i;
            int cnt = 0;
            for(k = 0; k < s.length(); k++){//从头比较
                int q = 0;
                int flag = 1;
                while(q < (j-i+1)){//从k起点,然后往后长度为j-i+1,比较已经选出的子串,比较完后起始点挪到下一个
                    if(s[i+q] != s[k+q]){
                        flag = 0;
                        break;
                    }
                    else
                        q++;
                }
                if(flag)
                    cnt++;
            }
            if(cnt >= 2){
                longest = max(longest,j-i+1);
            }
        }
    }
    cout << longest << endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值