标签:usaco
字符串处理。我是直接用的map记录的所有的子串,然后再从map中取出结果排序,然后就输出。其实这个题都是0-1串,开始的时候我是想着能不能用hash的方法来记录每个子串,但是后面觉得00 000 0000 这些情况不好处理,后来看了官网题解发现我们可以在这个串的最前面的加上一个1然后就可以用二进制hash的思想求解了。
PS:这道题的输出格式蛮坑的。。。。。。
代码如下
/*
ID:15674811
LANG:C++
PROG:contact
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 1000000
typedef struct
{
string str;
int cnt;
}P;
P p[maxn];
bool cmp(P p1,P p2)
{
int len1=p1.str.size();
int len2=p2.str.size();
if(p1.cnt==p2.cnt)
{
if(len1==len2)
{
return p1.str<p2.str;
}
else
{
return len1<len2;
}
}
return p1.cnt>p2.cnt;
}
string str,str1;
int main()
{
freopen("contact.in","r",stdin);
freopen("contact.out","w",stdout);
//freopen("lkl.txt","r",stdin);
map<string,int>M;
int a,b,n;
cin>>a>>b>>n;
str="";
while(cin>>str1)
{
str+=str1;
}
int len=str.size();
for(int i=0;i<len;i++)
{
for(int j=a;j<=b&&i+j<=len;j++)
{
str1=str.substr(i,j);
M[str1]++;
}
}
map<string,int>::iterator it;
int k=1;
for(it=M.begin();it!=M.end();it++)
{
p[k].str=it->first;
p[k].cnt=it->second;
k++;
}
sort(p+1,p+k,cmp);
int cnt=1,cur=p[1].cnt,tmp=1;
cout<<p[1].cnt<<endl;
cout<<p[1].str;
for(int i=2;i<k;i++)
{
if(p[i].cnt==p[i-1].cnt)
{
if(cnt==0)
cout<<p[i].str;
else
cout<<" "<<p[i].str;
cnt++;
if(cnt==6)
{
cout<<endl;
cnt=0;
}
}
else
{
if(tmp==n)
break;
if(cnt!=0)
cout<<endl;
cout<<p[i].cnt<<endl;
cout<<p[i].str;
cnt=1;
tmp++;
}
}
if(cnt!=0)
cout<<endl;
return 0;
}
标签:usaco
原文地址:http://blog.csdn.net/acm_lkl/article/details/45027433