zoukankan      html  css  js  c++  java
  • 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;
    }
     
  • 相关阅读:
    SqlServer 格式化时间
    工作生活两三事
    前端面试题准备 3
    前端面试题准备 2
    MYSQL---自定义函数
    MYSQL---MD5()、PASSWORD()函数
    MYSQL---DATE_ADD()
    MYSQL---%
    MYSQL---多表删除
    MYSQL---CREATE...SELECT
  • 原文地址:https://www.cnblogs.com/NINGLONG/p/7608116.html
Copyright © 2011-2022 走看看