给出字符串

来源:互联网 发布:js删除指定id的tr 编辑:程序博客网 时间:2024/06/10 18:51

Description

给出一个由小写字母组成的字符串。你的任务是找出其最长的出现至少两次的子串的长度。这些重复出现的子串可以重叠(参见样例2)。

Input

输入文件ygas.in第一行包含该字符串。数据保证该字符串非空,由小写字母组成,且其长度不超过100。

Output

输出文件ygas.out包含一个数代表至少出现两次的最长子串的长度。

Sample Input

【输入样例1】
abcd
【输入样例2】
ababa
【输入样例3】
zzz

Sample Output

【输出样例1】
0
【输出样例2】
3
【输出样例3】
3

分析
数据太水,直接暴力。

程序:

varzfc,s:string;max,n,tj,i,j,k:longint;function check(x:longint):boolean;vari:longint;begin    for i:=1 to length(s) do    if s[i]<>zfc[x+i-1] then exit(false);    exit(true);end;begin    assign(input,'ygas.in');    reset(input);    assign(output,'ygas.out');    rewrite(output);    read(zfc);max:=0;    n:=length(zfc);    for i:=1 to n-1 do    begin        for j:=1 to n do        begin            s:=copy(zfc,j,i);            tj:=0;            for k:=1 to n do            begin                if check(k)=true then inc(tj);                if tj=2 then                begin                    if length(s)>max then max:=length(s);                    break;                end;            end;        end;    end;    write(max);    close(input);    close(output);end.
原创粉丝点击