CS Round#49 C Max Substring

Posted NINGLONG

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CS Round#49 C Max Substring相关的知识,希望对你有一定的参考价值。

Max Substring

Time limit: 1000 ms
Memory limit: 256 MB

 

You are given a string S. Find a string T that has the most number of occurrences as a substring in S.

If the solution is not unique, you should find the one with maximum length. If the solution is still not unique, find the smallest lexicographical one.

Standard input

The first line contains string S.

Standard output

Print string T on the first line.

Constraints and notes

  • SS consists of lowercase letters of the English alphabet
  • The length of S is between 1 and 10^5?? 
 
题意:给定一个字符串,求它数量最多的子串中最长的一个,如有多组答案,则输出字典序最小的一个。
 
这题看起来是一个很高大上的字符串题,其实仔细一想就会发现,只要找出出现最多的字母之后往右扩展即可。
 
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100000+10
char s[MAXN];
int l,maxn=0,maxl,last,ans=-1,be,cnt[27];
vector<int>pos[27];
int main(){
    scanf("%s",s);
    l=strlen(s);
    for(int i=0;i<l;i++)cnt[s[i]-a]++;
    for(int i=0;i<26;i++)
        maxn=max(maxn,cnt[i]);        
    for(int t=0;t<26;t++){
        if(cnt[t]!=maxn)continue;
        for(int i=0;i<l;i++)
            if(s[i]-a==t)pos[s[i]-a].push_back(i),last=i;
        int maxlen=0;    
        for(int k=1;last+k<l;k++){
            char r=s[pos[t][0]+k];
            bool flag=true;
            for(int i=1;i<pos[t].size();i++)
                if(s[pos[t][i]+k]!=r){
                flag=false;
                break;
            }
            if(!flag)break;
            maxlen++;
        }
        if(maxlen>ans){
            ans=maxlen;
            be=last;
        }
    }
    for(int i=be;i<=be+ans;i++)putchar(s[i]);
    return 0;
}

 

 

以上是关于CS Round#49 C Max Substring的主要内容,如果未能解决你的问题,请参考以下文章

解题报告CF EDU #ROUND 110 A~C

Codeforces Round #656 (Div. 3) A. Three Pairwise Maximums(思维/构造)

Codeforces Round #610 (Div. 2) a/b/c

Educational Codeforces Round 49(A,B,C,D)

CS Round #50 Min Races(nlogn级别的LDS)

Codeforces Round #375 (Div. 2) ABCDE