zoukankan      html  css  js  c++  java
  • 面试宝典——求一个字符串中连续出现次数最多的子串

     1 #include"iostream"
     2 #include"stdio.h"
     3 #include"string.h"
     4 #include"vector"
     5 using namespace std;
     6 
     7 const int MAXN=100005;
     8 
     9 pair<int,string> fun(const string &str)
    10 {
    11     vector<string>subs;
    12     int maxcount=1,count=1;
    13     string substr;
    14     int i,len=str.length();
    15     for(i=0;i<len;i++)
    16     {
    17         subs.push_back(str.substr(i,len-i));
    18     }
    19     for(i=0;i<len;i++)
    20     {
    21         for(int j=i+1;j<=(len+i)/2;j++)
    22         {
    23                 count=1;
    24                 if(subs[i].substr(0,j-i)==subs[j].substr(0,j-i))
    25                 {
    26                     count++;
    27                     for(int k=j+(j-i);k<len;k+=j-i)
    28                     {
    29                         if(subs[i].substr(0,j-i)==subs[k].substr(0,j-i))
    30                             count++;
    31                         else
    32                             break;
    33                     }
    34                     if(count>maxcount)
    35                     {
    36                         maxcount=count;
    37                     }
    38                 }
    39         }
    40     }
    41     return make_pair(maxcount,substr);
    42 }
    43 
    44 pair<int,string> fun1(const string& str)
    45 {
    46     int maxcount=1,count=1;
    47     string substr;
    48     int i=0,j=0;
    49     int len=str.length();
    50     int k=i+1;
    51     while(i<len)
    52     {
    53         j=str.find(str[i],k);
    54         if(j==string::npos || j>(len+i)/2)
    55         {
    56             i++;
    57             k=i+1;
    58         }
    59         else
    60         {
    61             int s=i;
    62             int s1=j-i;
    63             while(str.substr(s,s1)==str.substr(j,s1))
    64             {
    65                 count++;
    66                 s=j;
    67                 j=j+s1;
    68             }
    69             if(count>maxcount)
    70             {
    71                 maxcount=count;
    72                 substr=str.substr(i,s1);
    73             }
    74             k=j+1;
    75             count=1;
    76         }
    77     }
    78     return make_pair(maxcount,substr);
    79 }
    80 
    81 int main()
    82 {
    83     string str;
    84     pair<int,string> rs;
    85     while(cin>>str)
    86     {
    87         rs=fun(str);
    88         cout<<rs.second<<":"<<rs.first<<endl;
    89         rs=fun1(str);
    90         cout<<rs.second<<":"<<rs.first<<endl;
    91     }
    92     return 0;
    93 }
    View Code
  • 相关阅读:
    一道网易面试题
    OC的引用计数
    ReplayKit2 采集音视频回调格式分析
    《剑指offer3- 从末尾到头打印链表》
    《剑指offer
    《剑指offer
    ReplayKit2:声音回调时间戳问题
    UILable在Autolayout模式下面自动调节字体大小
    建表手写语句
    oracle创建主键序列和在ibatis中应用
  • 原文地址:https://www.cnblogs.com/acm-jing/p/10369206.html
Copyright © 2011-2022 走看看