zoukankan      html  css  js  c++  java
  • 给出字符串

    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;
    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.
    
    
  • 相关阅读:
    1245. Tree Diameter
    771. Jewels and Stones
    830. Positions of Large Groups
    648. Replace Words
    647. Palindromic Substrings
    435. Non-overlapping Intervals
    646. Maximum Length of Pair Chain
    645. Set Mismatch
    242. Valid Anagram
    438. Find All Anagrams in a String
  • 原文地址:https://www.cnblogs.com/YYC-0304/p/9500063.html
Copyright © 2011-2022 走看看