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
思路:第一反应就是三进制,后来发现二进制加前导一还要好一些 3^12远远大于2^13 (囧啊),所以速度极慢。另吐槽一句,这个输出格式和输入格式也太蛋疼了吧,不认真仔细分析,就会粗心出错
Executing...
Test 1: TEST OK [0.000 secs, 9844 KB]
Test 2: TEST OK [0.000 secs, 9844 KB]
Test 3: TEST OK [0.000 secs, 9844 KB]
Test 4: TEST OK [0.000 secs, 9844 KB]
Test 5: TEST OK [0.259 secs, 9844 KB]
Test 6: TEST OK [0.670 secs, 9844 KB]
Test 7: TEST OK [0.691 secs, 9844 KB]
All tests OK.
1 /* 2 ID:wuhuaju2 3 PROG:contact 4 LANG:C++ 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <cstdlib> 9 #include <algorithm> 10 #include <cstring> 11 #include <string> 12 using namespace std; 13 14 15 const int maxn=200010,maxf=531442; 16 const int san[]={1,3,9,27,81,243,729,2187,6561,19683,59049,177147,531441}; 17 char s[maxn],s1[maxn]; 18 int t,last,a,b,l,n,cnt; 19 int f[maxf]; 20 bool flag; 21 22 struct cc 23 { 24 int a,b; 25 bool friend operator < (const cc &a,const cc &b) 26 { 27 if (a.b==b.b) 28 return a.a>b.a; 29 return a.b<b.b; 30 } 31 } c[maxf]; 32 33 void close() 34 { 35 fclose(stdin); 36 fclose(stdout); 37 exit(0); 38 } 39 40 void convert(int t,bool flag) 41 { 42 int x[13]; 43 int cnt=0; 44 while (t!=0) 45 { 46 cnt++; 47 x[cnt]=t % 3; 48 t=t / 3; 49 } 50 if (flag) 51 cout<<" "; 52 for (int i=cnt;i>=1;i--) 53 cout<<x[i]-1; 54 } 55 56 void print() 57 { 58 cnt=0; 59 for (int i=0;i<=san[12];i++) 60 if (f[i]>0) 61 { 62 cnt++; 63 c[cnt].a=i; 64 c[cnt].b=f[i]; 65 } 66 sort(c+1,c+cnt+1); 67 // for (int i=cnt;i>=1;i--) 68 // cout<<"c.a:"<<c[i].a<<" "<<"c.b:"<<c[i].b<<endl; 69 int tot=0; 70 flag=false; 71 for (int i=cnt;i>=1;i--) 72 { 73 flag=false; 74 tot=0; 75 cout<<c[i].b<<endl; 76 while (c[i].b==c[i-1].b) 77 { 78 tot++; 79 if (tot>=7) 80 { 81 flag=false; 82 tot=1; 83 cout<<endl; 84 } 85 convert(c[i].a,flag); 86 flag=true; 87 i--; 88 89 } 90 tot++; 91 if (tot>=7) 92 { 93 flag=false; 94 tot=1; 95 cout<<endl; 96 } 97 convert(c[i].a,flag); 98 flag=true; 99 cout<<endl; 100 n--; 101 if (n==0) 102 break; 103 } 104 // cout<<endl; 105 } 106 107 108 109 void work() 110 { 111 } 112 113 void init () 114 { 115 memset(f,0,sizeof(f)); 116 freopen("contact.in","r",stdin); 117 freopen("contact.out","w",stdout); 118 scanf("%d%d%d",&a,&b,&n); 119 while (scanf("%s",s1)!=EOF) 120 { 121 strcat(s,s1); 122 } 123 l=strlen(s); 124 last=0; 125 for (int i=a;i<=b;i++) 126 { 127 // cout<<"i:"<<i<<endl; 128 t=i;last=0; 129 for (int j=0;j<l;j++)// change !! 130 { 131 if (j>=i) 132 { 133 t=(s[j-i]-47)*san[i-1]; 134 t=last-t; 135 t=t*3+(s[j]-47); 136 // cout<<"j:"<<j<<" t:"<<t<<" "; 137 // convert(t); 138 f[t]++; 139 last=t; 140 } 141 else 142 { 143 t--; 144 last+=san[t]*(s[j]-47); 145 if (j+1==i) 146 f[last]++; 147 } 148 } 149 // cout<<"------------------"<<endl; 150 } 151 print(); 152 153 } 154 155 int main () 156 { 157 init(); 158 work(); 159 close(); 160 return 0; 161 }