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;
    }
     
  • 相关阅读:
    X11学习
    Linux 实用命令
    Notepad++ 添加右键菜单
    VS Code编译C/C++
    map与unordered_map的区别
    Rtt / vxworks 任务状态装换对比
    awesome computer vision repo
    英语每日金句
    不要在意七十亿分之一对另七十亿分之一的看法
    致知在格物,物格而后知至,知至而后意诚,意诚而后心正,心正而后身修,身修而后家齐,家齐而后
  • 原文地址:https://www.cnblogs.com/NINGLONG/p/7608116.html
Copyright © 2011-2022 走看看