[usaco3.1.4]contact
简单一个暴搜就能过了,但是我的双关键字快排我也是醉了(用c++的sort不会写双关键字排序,只好自己写,可是写得特别拙),代码混乱(看来我应该分开一个个函数写的)
/* ID:abc31261 LANG:C++ TASK:contact */ #include<cstdio> #include<cstring> #include<map> #include<algorithm> #include<iostream> using namespace std; const int maxn=1<<13; string ss[maxn]; map<string,int> mm; int len=0,num[maxn],r[maxn]; void dfs(int x,string s2) { if (x==0)ss[++len]=s2; else { dfs(x-1,s2+'0'); dfs(x-1,s2+'1'); } } bool check(int x,int y) { if (num[x]==num[y]) { if (ss[x].size()<ss[y].size())return true; if (ss[x].size()==ss[y].size() && ss[x]<ss[y])return true; } return false; } bool check1(int x,int y) { if (num[x]==num[y]) { if (ss[x].size()>ss[y].size())return true; if (ss[x].size()==ss[y].size() && ss[x]>ss[y])return true; } return false; } void quicksort(int x,int y) { int h=x,l=y,mid=r[(h+l)/2]; while (h<=l) { while (num[r[h]]>num[mid] || (check(r[h],mid)))h++; while (num[r[l]]<num[mid] || (check1(r[l],mid)))l--; if (h<=l) { swap(r[h],r[l]); h++; l--; } } if (h<y)quicksort(h,y); if (l>x)quicksort(x,l); } int main() { string s="",s1; int i,j,l,a,b,n; freopen("contact.in","r",stdin); freopen("contact.out","w",stdout); scanf("%d%d%d",&a,&b,&n); while (cin>>s1)s+=s1; for (i=a;i<=b;i++)dfs(i,""); //构造所有的01序列 for (i=1;i<=len;i++)mm[ss[i]]=r[i]=i; for (i=0;i<=s.size()-a;i++) { s1=""; for (j=i;j<i+a-1;j++)s1+=s[j]; for (j=i+a-1;j<s.size() && j<=i+b;j++) { s1+=s[j]; //逐个枚举 num[mm[s1]]++; } } quicksort(1,len); //快排 i=0; j=0; while (++j<=n && ++i<=len && num[r[i]]!=0) { l=1; cout<<num[r[i]]<<endl<<ss[r[i]]; while (i+1<=len && num[r[i+1]]==num[r[i]]) { if (l%6!=0)cout<<" "; //输出控制 cout<<ss[r[++i]]; if ((++l)%6==0)cout<<endl; } if (l%6!=0)cout<<endl; } return 0; }