标签:length define contain lower note not i++ lin cal
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.
The first line contains string S.
Print string T on the first line.
#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; }
标签:length define contain lower note not i++ lin cal
原文地址:http://www.cnblogs.com/NINGLONG/p/7608116.html