USACO3,1,5这道题我的输出和他的一样,为什么判错啊?
http://www.//当成二进制处理,前面加1.
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<string.h>
char s1[200]={0};
struct node{
int num,sta;
};
struct node ars[200000];
void final(int m){ //当一个整数转化成对应的字符串,除去前面的1.
int f=0,i,p=0;
char ars1[20]={0};
while(m){
ars1[f]=(m%2)-1+'1';
m=m/2;
f++;
}
for(i=f-2;i>=0;i--)
s1[p]=ars1[i],p++;
}
int deal(int i ,int j,char s[]){//从i 位开始总共j位数对应的整数
int sum=0,t,m,n;
if(s[i+j-1]){
for(t=0;t<j;t++){
n=s[i+t]&0x0f;
m=pow(2,j-1-t);
sum+=(int)m*n;
}
sum+=(int)pow(2,j);
}
return sum;
}
int get(int low,int high,struct node ars[]){ //快排
int num=ars[low].num,sta=ars[low].sta;
while(low<high){
while(low<high&&ars[high].sta<=sta) high--;
ars[low].num=ars[high].num,ars[low].sta=ars[high].sta;
while(low<high&&ars[low].sta>=sta) low++;
ars[high].num=ars[low].num,ars[high].sta=ars[low].sta;
}
ars[low].num=num,ars[low].sta=sta;
return low;
}
void sort(int low,int high,struct node ars[]){ //快排
int p=get(low,high,ars);
if(low<high){
sort(low,p,ars);
sort(p+1,high,ars);
}
}
void sort1(int fans[],int n){ //对于相同频率的,按从小到大排列出来
int i ,j,t;
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(fans[j]>fans[j+1])
t=fans[j],fans[j]=fans[j+1],fans[j+1]=t;
}
int main()
{
FILE *fin=fopen("contact.in","r");
FILE *fout=fopen("contact.out","w");
int a,b,n,t,cnt=0,why,fir;
int i ,j,f=0;
int ans[200000]={0},fans[10]={0};
char c[100]={0},s[400050]={0};
fscanf(fin,"%d %d %d",&a,&b,&n);
while(fscanf(fin,"%s",c)!=EOF){
for(i=0;c[i];i++)
s[f]=c[i],f++;
memset(c,0,sizeof(c));
}
for(i=0;s[i];i++)
for(j=a;j<=b;j++){
t=deal( i, j, s);
ans[t]++;//求出相同字符串出现的频率
}
t=(int)pow(2,b+2);
fir=(int)pow(2,a);
for(i=fir;i<t;i++)
ars[i].num=i,ars[i].sta=ans[i];
sort(fir,t-1,ars);
for(i=fir,why=0;ars[i].sta&&why<n;){
cnt=0,f=0;
fprintf(fout,"%d\n",ars[i].sta);
while(ars[i-1].sta==ars[i].sta||cnt==0){
fans[f]=ars[i].num,f++;
i++;
cnt++;
}
sort1(fans,f);
for(j=0;j<f-1;j++){
memset(s1,0,sizeof(s1));
final(fans[j]);
if(!((j+1)%6)) fprintf(fout,"%s\n",s1);
else fprintf(fout,"%s ",s1);
}
memset(s1,0,sizeof(s1));
final(fans[f-1]);
fprintf(fout,"%s\n",s1);
memset(fans,0,sizeof(fans));
while(ars[i-1].sta==ars[i].sta) i++;
why++;
}
return 0;
}