Codeforces Round #434 (Div. 2)【A、B、C、D】

Codeforces Round #434 (Div. 2)ide

 

codeforces 858A. k-rounding【水】spa

題意:已知n和k,求n的最小倍數x,要求x後綴至少有k個0。3d

題解:答案就是10^k和n的最小公倍數。code

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cmath>
 5 using namespace std;
 6 typedef long long ll;
 7 ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
 8 int main() {
 9     ll n, k, s=1;
10     scanf("%lld %lld", &n, &k);
11     while(k--) s *= 10;
12     ll t = gcd(n, s);
13     printf("%lld\n", n / t * s);
14     return 0;
15 }
15ms

codeforces 858B. Which floor? 【暴力】blog

題意:已知每層樓的房間數量相同但不知道具體數目,從一樓往上依次給每一個房間從小到大編號(從1號開始),如今給出m個房間的信息(房間號和所在樓層),求第n號房間所在樓層,如有多解則輸出-1。索引

題解:暴力,維護每層樓的可能的最小、最大房間數。字符串

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int main() {
 6     int n, m, k, f;
 7     int mi=1, ma=100;
 8     scanf("%d%d", &n, &m);
 9     while(m--) {
10         scanf("%d%d", &k, &f);
11         if(f>1) ma = min(ma, (k-1)/(f-1));
12         mi = max(mi, (k+f-1)/f);
13     }
14     //printf("%d %d\n", mi, ma);
15     if((f=(n+mi-1)/mi) != (n+ma-1)/ma) puts("-1");
16     else printf("%d\n", f);
17     return 0;
18 }
15ms

codeforces 858C. Did you mean...【水】get

題意:給你一個字符串,如今要你給其中加空格隔開單詞,使得每一個單詞不能有連續三個以上不一樣的輔音字母,輸出加的空格最少的字符串。string

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<map>
 5 using namespace std;
 6 const int N = 3001;
 7 char s[N], t[N];
 8 map<char, int> mp;
 9 int main() {
10     mp['a'] = mp['e'] = mp['i'] = mp['o'] = mp['u'] = 1;
11     int i, len;
12     gets(s);
13     len = strlen(s);
14     if(len < 3) {puts(s); return 0;}
15     int cnt = 0;
16     t[cnt++] = s[0]; t[cnt++] = s[1];
17     for(i = 2; i < len; ++i) {
18         if(!mp[s[i]] && !mp[s[i-1]] && !mp[s[i-2]] &&
19            (s[i]!=s[i-1] || s[i-1] != s[i-2])) {
20             t[cnt++] = ' '; t[cnt++] = s[i];
21             s[i-1] = s[i-2] = 'a';
22         }
23         else t[cnt++] = s[i];
24     }
25     t[cnt++] = '\0';
26     puts(t);
27     return 0;
28 }
31ms

待補。。it

codeforces 858D. Polycarp's phone book【字典樹】

題意:有n個不一樣的九位數的電話號碼(非0開頭),求每一個電話號的最短的能惟一索引該號碼的子串。(輸入保證全部號碼不一樣)

題解:將每一個字符串的全部後綴插入字典樹中,對每一個號碼的查詢就先把其全部後綴刪除,而後對其全部後綴查找其前綴出現的次數爲0的最短號碼即爲答案。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int N = 70010;
 6 const int M = 10;
 7 const int len = 9;
 8 char a[N][M];
 9 struct Trie {
10     int next[M];
11     int cnt;
12     void init() {
13         cnt = 0;
14         memset(next, -1, sizeof(next));
15     }
16 }T[3150005];
17 int le;
18 void inser(char *s) {
19     int i = 0, p = 0;
20     while(s[i]) {
21         int id = s[i] - '0';
22         if(T[p].next[id] == -1) {
23             T[le].init();
24             T[p].next[id] = le++;
25         }
26         p = T[p].next[id];
27         T[p].cnt++;
28         i++;
29     }
30 }
31 void add(char *s) {
32     int i = 0, p = 0;
33     while(s[i]) {
34         int id = s[i] - '0';
35         p = T[p].next[id];
36         T[p].cnt++;
37         i++;
38     }
39 }
40 void del(char *s) {
41     int i = 0, p = 0;
42     while(s[i]) {
43         int id = s[i] - '0';
44         p = T[p].next[id];
45         T[p].cnt--;
46         i++;
47     }
48 }
49 int query(char *s) {
50     int i = 0, p = 0;
51     while(s[i]) {
52         int id = s[i] - '0';
53         p = T[p].next[id];
54         if(T[p].cnt == 0) return i;
55         i++;
56     }
57     return 11;
58 }
59 int main() {
60     int n, m, i, j, mi, t, l, r;
61     scanf("%d", &n);
62     le = 1;
63     T[0].init();
64     for(i = 1; i<= n; ++i) {
65         scanf("%s", a[i]);
66         for(j = 0; j < len; ++j) inser(a[i]+j);
67     }
68     for(i = 1; i<= n; ++i) {
69         mi = 11;
70         for(j = 0; j < len; ++j) del(a[i]+j);
71         for(j = 0; j < len; ++j) {
72             t = query(a[i]+j);
73             if(t < mi) {mi = t; l = j; r = j+t;}
74         }
75         for(j = 0; j < len; ++j) add(a[i]+j);
76         for(j = l; j <= r; ++j) printf("%c", a[i][j]);
77         puts("");
78     }
79     return 0;
80 }
186ms
相關文章
相關標籤/搜索