You're Given a String... CodeForces

来源:互联网 发布:剑灵捏脸数据分享 编辑:程序博客网 时间:2024/04/27 23:48

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;}


原创粉丝点击