USACO3.1.5Contact

Contact
IOI'98

The cows have developed a new interest in scanning the universe outside their farm with radiotelescopes. Recently, they noticed a very curious microwave pulsing emission sent right from the centre of the galaxy. They wish to know if the emission is transmitted by some extraterrestrial form of intelligent life or if it is nothing but the usual heartbeat of the stars.

Help the cows to find the Truth by providing a tool to analyze bit patterns in the files they record. They are seeking bit patterns of length A through B inclusive (1 <= A <= B <= 12) that repeat themselves most often in each day's data file. They are looking for the patterns that repeat themselves most often. An input limit tells how many of the most frequent patterns to output.

Pattern occurrences may overlap, and only patterns that occur at least once are taken into account.

PROGRAM NAME: contact

INPUT FORMAT

Line 1: Three space-separated integers: A, B, N; (1 <= N < 50)
Lines 2 and beyond: A sequence of as many as 200,000 characters, all 0 or 1; the characters are presented 80 per line, except potentially the last line.

SAMPLE INPUT (file contact.in)

2 4 10
01010010010001000111101100001010011001111000010010011110010000000

In this example, pattern 100 occurs 12 times, and pattern 1000 occurs 5 times. The most frequent pattern is 00, with 23 occurrences.

OUTPUT FORMAT

Lines that list the N highest frequencies (in descending order of frequency) along with the patterns that occur in those frequencies. Order those patterns by shortest-to-longest and increasing binary number for those of the same frequency. If fewer than N highest frequencies are available, print only those that are.

Print the frequency alone by itself on a line. Then print the actual patterns space separated, six to a line (unless fewer than six remain).

SAMPLE OUTPUT (file contact.out)

23
00
15
01 10
12
100
11
11 000 001
10
010
8
0100
7
0010 1001
6
111 0000
5
011 110 1000
4
0001 0011 1100

 题解:一看题目,果断想到位运算。。。我的想法是,枚举所有在范围长度内的字符串,并转换成数字,然后用统计数组统计相应数字出现的次数。但是这里有一个问题,那就是字符串有首位有0,直接转换成数字的话,是不能表示出来的。例如011和11,转换之后就是同一数字了,所以我们还要进行处理一下,那就是枚举出来的字符串在首位添加一个字符’1‘。这样就完美解决了首位是0的问题了。最后的问题就是把统计次数的数组排下序就OK了,并注意输出格式。

丑陋的代码o(╯□╰)o:

View Code
  1 /*
  2 ID:spcjv51
  3 PROG:contact
  4 LANG:C
  5 */
  6 #include<stdio.h>
  7 #include<string.h>
  8 #include<ctype.h>
  9 #include<math.h>
 10 int f[10000],lens[10000],p[10000];
 11 char s[200005],s1[10000][15];
 12 int len;
 13 void qsort(int l,int r)
 14 {
 15     char sb[15];
 16     int i,j,mid,midlen,temp,mid2;
 17     i=l;
 18     j=r;
 19     mid=f[(l+r)>>1];
 20     midlen=lens[(l+r)>>1];
 21     mid2=p[(l+r)>>1];
 22     while(i<=j)
 23     {
 24         while(f[i]>mid||(f[i]==mid&&lens[i]<midlen)||(f[i]==mid&&lens[i]==midlen&&p[i]<mid2)) i++;
 25         while(f[j]<mid||(f[j]==mid&&lens[j]>midlen)||(f[j]==mid&&lens[j]==midlen&&p[j]>mid2)) j--;
 26         if(i<=j)
 27         {
 28             temp=f[i];
 29             f[i]=f[j];
 30             f[j]=temp;
 31             temp=lens[i];
 32             lens[i]=lens[j];
 33             lens[j]=temp;
 34             temp=p[i];
 35             p[i]=p[j];
 36             p[j]=temp;
 37             strcpy(sb,s1[i]);
 38             strcpy(s1[i],s1[j]);
 39             strcpy(s1[j],sb);
 40             i++;
 41             j--;
 42         }
 43     }
 44     if(i<r) qsort(i,r);
 45     if(j>l) qsort(l,j);
 46 }
 47 int main(void)
 48 {
 49     char ss[15],s3[15];
 50     freopen("contact.in","r",stdin);
 51     freopen("contact.out","w",stdout);
 52     long i,j,k,m,maxs,t,h;
 53     int a,b,n,r,ans,sum;
 54     char c;
 55     scanf("%d%d%d",&a,&b,&n);
 56     i=-1;
 57     maxs=0;
 58     memset(f,0,sizeof(f));
 59     memset(lens,0,sizeof(lens));
 60     memset(p,0,sizeof(p));
 61     while(scanf("%c",&c)!=EOF)
 62         if(isdigit(c))
 63         {
 64             i++;
 65             s[i]=c;
 66         }
 67     len=strlen(s);
 68     for(i=a; i<=b; i++)
 69         for(j=0; j<len-i+1; j++)
 70         {
 71             m=0;
 72             ss[m]='1';
 73             sum=0;
 74             k=j;
 75             while(k<j+i)
 76             {
 77                 m++;
 78                 ss[m]=s[k];
 79                 k++;
 80             }
 81             for(k=0; k<=m; k++)
 82             {
 83                 r=ss[k]-'0';
 84                 sum+=r*(1<<(m-k));
 85 
 86             }
 87             if(!f[sum])
 88             {
 89                 k=j;
 90                 m=-1;
 91                 while(k<j+i)
 92                 {
 93                     m++;
 94                     s1[sum][m]=s[k];
 95                     k++;
 96                 }
 97                 lens[sum]=strlen(s1[sum]);
 98                 k=0;
 99                 while(s1[sum][k]=='0') k++;
100                 h=-1;
101                 for(t=k; t<=m; t++)
102                 {
103                     h++;
104                     s3[h]=s1[sum][t];
105                 }
106                 ans=0;
107                 for(k=0; k<=h; k++)
108                     ans+=(s3[k]-'0')*(1<<(h-k));
109                 p[sum]=ans;
110 
111             }
112             f[sum]++;
113             if(sum>maxs) maxs=sum;
114 
115         }
116     qsort(0,maxs);
117     j=0;
118     for(i=1; i<=n; i++)
119     {
120         t=1;
121         if (f[j]==0) break;
122         ans=f[j];
123         printf("%d\n",ans);
124         printf("%s",s1[j]);
125         j++;
126         while(f[j]==ans)
127         {
128             t++;
129             if(t%6==1) printf("%s",s1[j]);
130             else
131                 printf(" %s",s1[j]);
132             if(t%6==0) printf("\n");
133             j++;
134         }
135         if(t%6!=0) printf("\n");
136     }
137 
138     return 0;
139 }
原文地址:https://www.cnblogs.com/zjbztianya/p/2917888.html