计算重复字符串长度

请从字符串中找出至少重复一次的子字符串的最大长度

输入描述:

字符串,长度不超过1000

输出描述:

重复子串的长度,不存在输出0

示例1

输入

ababcdabcefsgg

输出

3

说明

abc为重复的最大子串,长度为3
#include <iostream>
#include <string>
 
using namespace std;
 
int statLen(string str, int i, int j) {
  int cur_len = 0;
  while (i < str.size() && j < str.size() && str[i] == str[j]) {// 判断子串
    i++;
    j++;
    cur_len++;
  }
  return cur_len;
}
int naiveLRS(string str) {
  int maxlen = 0;
  // 遍历所有字符串
  for (int i = 0; i != str.size(); ++i) {
    int len = 0;
    for (int j = i + 1; j != str.size(); j++) {
      len = statLen(str, i, j);// 获取子串长度
      if (maxlen < len) { // 记录最大长度
        maxlen = len;
      }
    }
  }
  return maxlen;
}
int main() {
  string str;
  cin >> str;
  printf("%d", naiveLRS(str));
}
import java.util.*
public class Main{
    private static int statLen(String X,int k,int j){
        int cur_len = 0;
        while(k < X.length()&&j<X.length()&&X.charAt(k) == X.charAt(j)){
            k++;
            j++;
            cur_len++;
        }
        return cur_len;
    }

    public static int nativeLRS(String x){
        int maxlen = 0;
        int length = x.length();
        for(int i = 0; i < length; i++){
            int len = 0;
            int k = i;
            for(int j = i+1;j<length;j++){
                len = staLen(x,k,j);
                if(maxlen < len)
                    maxlen = len;
            }   
        }
        return maxlen;
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String X = sc.nextLine();
        System.out,println(nativeLRS(X));
    }
}

 

posted @ 2019-03-03 09:28  strawqqhat  阅读(184)  评论(0编辑  收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }