码迷,mamicode.com
首页 > 其他好文 > 详细

ecjtu-summer training #12

时间:2017-08-03 18:09:44      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:while   前缀   describe   for   sizeof   打包   cto   target   turn   

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("\n");
37         else flag = true;
38         printf("%d\n",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("\n");
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
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
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
aaaaa
Sample 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\n",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 \n",ans,(x+1));
46             }
47             dfs(p1,x+1);
48         }
49     }
50     return ;
51 }
52 
53 int main() {
54     ios::sync_with_stdio(false);
55     int t, n;
56     cin>>t;
57     while(t--) {
58         Nod *root = new Nod;
59         cin>>n;
60         for(int i = 0; i < n; i ++) {
61             cin>>str;
62             len = strlen(str);
63             mkTree(root,str);
64         }
65         MAX = 0;
66         dfs(root, 0);
67         printf("%d\n",MAX);
68     }
69     return 0;
70 }

F - Triangle Fun

传送

ecjtu-summer training #12

标签:while   前缀   describe   for   sizeof   打包   cto   target   turn   

原文地址:http://www.cnblogs.com/xingkongyihao/p/7281078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!