• 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;
    }
     
  • 相关阅读:
    [codeforces] 97B Superset || 平面分治
    [hdu] 5696 区间的价值 || 序列分治
    [zoj] 1937 [poj] 2248 Addition Chains || ID-DFS
    [poj] 2286 The Rotation Game || ID-DFS
    [codeforces] 25E Test || hash
    luogu P1196 银河英雄传说
    luogu P1357 花园
    luogu P1156 垃圾陷阱
    luogu P1127 词链
    luogu P1131 时态同步
  • 原文地址:https://www.cnblogs.com/NINGLONG/p/7608116.html
Copyright © 2020-2023  润新知