求一个字符串的最大重复字串

就是一个字符串如“banana":最大重复字串就是"ana",出现了至少2次且最长。如果最长字符串有多个,那么方法一取的是第一次遇到的,方法二取得是多首字母开头最小的。如果没有重复的,那么返回thers is no repeat string!

Code:
  1. public class findMaxSubstring   
  2.     {   
  3.         class Program   
  4.         {   
  5.             static void Main(string[] args)   
  6.             {   
  7.   
  8.                 string s1;   
  9.                 s1 = "明知道天要下雨就该带把伞,明知道不会有结果就请别开始!";   
  10.                 Method1(s1);   
  11.                 Method2(s1);   
  12.             }   
  13.   
  14.             private static void Method2(string s1)   
  15.             {   
  16.                 string[] strings = new string[s1.Length];   
  17.                 //生成后缀数组   
  18.                 for (int i = 0; i < s1.Length; i++)   
  19.                 {   
  20.                     strings[i] = s1.Substring(i);   
  21.                 }   
  22.                 //后缀数组排序   
  23.                 for (int i = 0; i < s1.Length - 1; i++)   
  24.                 {   
  25.                     for (int j = 0; j < s1.Length - 1; j++)   
  26.                     {   
  27.                         if (strings[j].CompareTo(strings[j + 1]) == 1)   
  28.                         {   
  29.                             string s;   
  30.                             s = strings[j];   
  31.                             strings[j] = strings[j + 1];   
  32.                             strings[j + 1] = s;   
  33.                         }   
  34.                     }   
  35.                 }   
  36.                 //求最大重复字串   
  37.                 int max = 0, index = 0, a, b, n;   
  38.                 for (int i = 0; i < s1.Length - 1; i++)   
  39.                 {   
  40.                     n = 0;   
  41.                     while (n < strings[i].Length && n < strings[i + 1].Length && strings[i].Substring(0, n+1).Equals(strings[i + 1].Substring(0, n+1)))   
  42.                     {   
  43.                         n++;   
  44.                         if (n > max)   
  45.                         {   
  46.                             max = n;   
  47.                             index = i;   
  48.                         }   
  49.                     }   
  50.                 }    
  51.                 if (max == 0)   
  52.                 {   
  53.                     Console.WriteLine("方法2:maxlenth={0}", max);   
  54.                     Console.WriteLine("there is no repeat string!");   
  55.                 }   
  56.                 else  
  57.                 {   
  58.                     Console.WriteLine("方法2:maxlenth={0}", max);   
  59.                     Console.WriteLine(strings[index].Substring(0, max));   
  60.                 }   
  61.             }   
  62.   
  63.             private static void Method1(string s1)   
  64.             {   
  65.                 int max = 0, index = 0, a, b, n;   
  66.                 for (int i = 0; i < s1.Length; i++)   
  67.                     for (int j = i + 1; j < s1.Length; j++)   
  68.                     {   
  69.                         n = 0;   
  70.                         a = i; b = j;   
  71.                         while (b < s1.Length && s1.Substring(a, 1).Equals(s1.Substring(b, 1)))   
  72.                         {   
  73.                             n++;   
  74.                             a++;   
  75.                             b++;   
  76.                         }   
  77.                         if (n > max)   
  78.                         {   
  79.                             max = n;   
  80.                             index = j;   
  81.                         }   
  82.                     }   
  83.                 if (max == 0)   
  84.                 {   
  85.                     Console.WriteLine("方法1:maxlenth={0}", max);   
  86.                     Console.WriteLine("there is no repeat string!");   
  87.                 }   
  88.                 else  
  89.                 {   
  90.                     Console.WriteLine("方法1:maxlenth={0}", max);   
  91.                     Console.WriteLine(s1.Substring(index, max));   
  92.                 }   
  93.             }   
  94.         }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值