给出字符串
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
分析
數據太水,直接暴力。
程序:
var zfc,s:string; max,n,tj,i,j,k:longint;function check(x:longint):boolean; var i:longint; beginfor i:=1 to length(s) doif s[i]<>zfc[x+i-1] then exit(false);exit(true); end; beginassign(input,'ygas.in');reset(input);assign(output,'ygas.out');rewrite(output);read(zfc);max:=0;n:=length(zfc);for i:=1 to n-1 dobeginfor j:=1 to n dobegins:=copy(zfc,j,i);tj:=0;for k:=1 to n dobeginif check(k)=true then inc(tj);if tj=2 thenbeginif length(s)>max then max:=length(s);break;end;end;end;end;write(max);close(input);close(output); end.轉載于:https://www.cnblogs.com/YYC-0304/p/9500063.html
總結
- 上一篇: 最短路计数(spfa)
- 下一篇: FBI序列