A - The Trip, 2007
小包可以放在打包里,求最少的数量。
做法就是求出现相同数字最多的。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <string> 6 #include <queue> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #define ll long long 11 #define INF 0x3f3f3f3f 12 #define lowbit(x) x&(-x) 13 #define N 10010 14 #define M 110 15 using namespace std; 16 int a[N]; 17 int main() { 18 ios::sync_with_stdio(false); 19 int n; 20 bool flag = false; 21 while(cin>>n) { 22 if(n == 0) break; 23 memset(a,0,sizeof(a)); 24 for(int i = 1; i <= n; i ++) cin >> a[i]; 25 sort(a+1,a+n+1); 26 int k = -1, ans = 1; 27 for(int i = 2; i <= n; i ++) { 28 if(a[i] == a[i-1]) { 29 ans++; 30 if(i == n && ans > k) k = ans; 31 }else { 32 if(ans > k) k = ans; 33 ans = 1; 34 } 35 } 36 if(flag) printf(" "); 37 else flag = true; 38 printf("%d ",k); 39 for(int i = 1; i <= k; i ++) { 40 bool flag1 = false; 41 for(int j = i; j <= n; j += k) { 42 if(!flag1) { 43 printf("%d",a[j]); 44 flag1 = true; 45 }else printf(" %d",a[j]); 46 } 47 printf(" "); 48 } 49 } 50 return 0; 51 }
B - Partitioning by Palindromes
C - Seek the Name, Seek the Fame
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:
Step1. Connect the father's name and the mother's name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).
Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)
Input
Step1. Connect the father's name and the mother's name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).
Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)
The input contains a number of test cases. Each test case
occupies a single line that contains the string S described above.
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Output
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
For each test case, output a single line with integer numbers in
increasing order, denoting the possible length of the new baby's name.
Sample Input
ababcababababcabab aaaaaSample Output
2 4 9 18 1 2 3 4 5
KMP模板题。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 char str[400010]; 6 int nex[400010]; 7 int a[400010]; 8 void init(){ 9 int j = nex[0] = -1, i = 0; 10 int len = strlen(str); 11 while(i < len){ 12 if(j == -1 || str[i] == str[j]) nex[++i] = ++j; 13 else j = nex[j]; 14 } 15 } 16 int main(){ 17 while(scanf("%s",str)!=EOF){ 18 init(); 19 int k = 0,len = strlen(str); 20 int i = len; 21 while(nex[i]){ 22 a[k++] = nex[i]; 23 i = nex[i]; 24 } 25 for(int i = k-1; i >= 0; i --){ 26 printf("%d ",a[i]); 27 } 28 printf("%d ",len); 29 } 30 return 0; 31 }
D - Counting Triangles
E - Hyper Prefix Sets
英语是硬伤啊,看不懂题目,快结束时才看懂,可惜没时间了。
就是给定n个字符串,求前缀相同的数量乘以长度的最大值。
比如第一组数据。{0000,0001,10101,010},最大值是求前缀000,这时有两个,所以是6.
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <string> 6 #include <queue> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #define ll long long 11 #define INF 0x3f3f3f3f 12 #define lowvit(x) x&(-x) 13 #define N 50010 14 #define M 210 15 using namespace std; 16 char str[M]; 17 struct Nod { 18 int num; 19 Nod * next[11]; 20 Nod(){ 21 for(int i = 0; i < 10; i ++) 22 next[i] = NULL; 23 num = 0; 24 } 25 }; 26 int len; 27 void mkTree(Nod *p,char *str) { 28 for(int i = 0; i < len; i ++) { 29 int x = str[i] - '0'; 30 if(p->next[x] == NULL) { 31 p->next[x] = new Nod; 32 } 33 p = p->next[x]; 34 p->num++; 35 } 36 } 37 int MAX = -1; 38 void dfs(Nod *p, int x) { 39 for(int i = 0; i < 10; i ++) { 40 if(p->next[i] != NULL) { 41 Nod *p1 = p->next[i]; 42 int ans = p1->num; 43 if(MAX < ans*(x+1)) { 44 MAX = ans*(x+1); 45 // printf("%d %d ",ans,(x+1)); 46 } 47 dfs(p1,x+1); 48 } 49 } 50 return ; 51 } 52 void dele(Nod *p) { 53 for(int i = 0; i < 10; i ++) { 54 if(p->next[i]) 55 dele(p->next[i]); 56 } 57 free(p); 58 return; 59 } 60 int main() { 61 ios::sync_with_stdio(false); 62 int t, n; 63 cin>>t; 64 while(t--) { 65 Nod *root = new Nod; 66 cin>>n; 67 for(int i = 0; i < n; i ++) { 68 cin>>str; 69 len = strlen(str); 70 mkTree(root,str); 71 } 72 MAX = 0; 73 dfs(root, 0); 74 printf("%d ",MAX); 75 dele(root); 76 } 77 return 0; 78 }