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;
}
时间: 2024-08-08 11:17:40

CS Round#49 C Max Substring的相关文章

CodeForce Round#49 untitled (Hdu 5339)

Untitled Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 947    Accepted Submission(s): 538 Problem Description There is an integer a and n integers b1,…,bn. After selecting some numbers from b1

Manacher BestCoder Round #49 ($) 1002 Three Palindromes

题目传送门 1 /* 2 Manacher:该算法能求最长回文串,思路时依据回文半径p数组找到第一个和第三个会文串,然后暴力枚举判断是否存在中间的回文串 3 另外,在原字符串没啥用时可以直接覆盖,省去一个数组空间,位运算 >>1 比 /2 速度快,用了程序跑快200ms左右,位运算大法好 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1

Codeforces Beta Round #49 (Div. 2)

Codeforces Beta Round #49 (Div. 2) http://codeforces.com/contest/53 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #define

BestCoder Round #49

呵呵哒,1001的dfs返回值写错,wa了两发就没分了,1002显然是PAM可是我没学过啊!!!压位暴力可不可以...看看范围貌似不行,弃疗...1003根本不会做,1004想了想lcc发现不可做,那就是仙人掌分治,没写完囧... 最后Rating+69滚粗了... 官方题解: 1001 Untitled 对于一组可能的答案cc,如果先对一个觉小的c_ic?i??取模,再对一个较大的c_jc?j??取模,那么这个较大的c_jc?j??肯定是没有用的.因此最终的答案序列中的cc肯定是不增的.那么就

BestCoder Round #49 ($) 1001 Untitled

5339  Untitled 问题描述 有一个整数aa和nn个整数b_1, \ldots, b_nb?1??,-,b?n??.在这些数中选出若干个数并重新排列,得到c_1, \ldots, c_rc?1??,-,c?r??.我们想保证a \ mod \ c_1 \ mod \ c_2 \ mod \ldots \ mod \ c_r = 0a mod c?1?? mod c?2?? mod- mod c?r??=0.请你得出最小的rr,也就是最少要选择多少个数字.如果无解,请输出-1?1. 输入

Cs Round#56 D Find Path Union

题意:有一棵如下的完全二叉树,求所有给定结点到根节点的路径的并有多少条边. 一开始联想到线段树,发现结点的排布很像线段树的标号.于是模仿线段树敲了一下,交上去发现3个点MLE了... 无心优化,跑去看题解.题解的思路是自底向上,先将询问的结点排序,最后从最深的节点开始往上递推,记录每层开始分叉的结点,统计答案即可 正解: #include<bits/stdc++.h> using namespace std; typedef long long LL; vector<LL>a; q

Educational Codeforces Round 49 (Rated for Div. 2) ABCD

A. Palindromic Twist You are given a string ss consisting of nn lowercase Latin letters. nn is even. For each position ii (1≤i≤n1≤i≤n) in string ss you are required to change the letter on this position either to the previous letter in alphabetic ord

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

A Palindromic Twist 字符串模拟,暴力check下. 1 #include <set> 2 #include <map> 3 #include <queue> 4 #include <deque> 5 #include <stack> 6 #include <cmath> 7 #include <cstdio> 8 #include <vector> 9 #include <string

BC round 49 A Untitled

本来是坐了两题的,结果第二题被黑了,两题的时候排名room第三,真是吓到我了,说实话这次真是认真做的,结 果还不错虽然只过了一题,貌似没几个人过两题,BC就是这么坑,老是出这么难的题,而且这两次我做的都是递归结出来的,还都是三次递归...好多人爆零,我还是一如既往的一道题,好像从来都是一道题...继续努力 认真做题,认真做题,认真做题(重要的事说三遍) 思路: 这道题我没有排序,直接递归的因为层数是递增的,所以可以优先找到层数最少的,每次对小于当前的数取余就ok啦,然后继续递归它的余数,直到为0