思路:抽出子串,暴力+结构体排序 n<=60,最多只会有1830种结果
代码:
#include<iostream>#include<cstring>#include<cstdio>#include<string>#include<algorithm>using namespace std;const int MAXN=2000;struct node{ string str; int len; int cnt; int pos; node() { str=""; len=0; cnt=0; pos=0; }}ans[MAXN];bool cmp(node a, node b){ if(a.cnt>b.cnt) return true; else if(a.cnt==b.cnt) { if(a.len>b.len) return true; else if(a.len==b.len) { return a.pos<b.pos; } else { return false; } } else { return false; }}void slove(int l, string s){ int length=s.length(); int countnumber=0; for(int i=l;i<=length;i++) { for(int j=0;j<=length-i;j++) { string ss=s.substr(j,i); int flag=0; for(int k=0;k<=countnumber;k++) { if(ans[k].str==ss) { ans[k].cnt++; flag=1; break; } } if(!flag) { ans[countnumber].str=ss; ans[countnumber].len=ss.length(); ans[countnumber].cnt=1; ans[countnumber].pos=countnumber; countnumber++; } } } sort(ans,ans+countnumber+1,cmp); cout<<ans[0].str<<endl;}int main(){ string s; int l; scanf("%d",&l); cin>>s; slove(l,s); return 0;}
新闻热点
疑难解答