[关闭]
@Dmaxiya 2018-08-17T10:19:30.000000Z 字数 5450 阅读 1022

Codeforces Round #459 (Div. 2)

Codeforces


Contests 链接:Codeforces Round #459 (Div. 2)
过题数:4
排名:75/10531

A. Eleven

题意

输出一个长度为 的字符串,如果 是斐波那契数列中的某个数字,则这个字符串的第 位为 ,否则它的第 位为

输入

输入只包含一个整数

输出

输出所求字符串。

样例

输入 输出
8 OOOoOooO
15 OOOoOooOooooOoo

题解

预处理出 以内的斐波那契数列,每次二分查找数列中是否存在 来判断输出 还是

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cfloat>
  7. #include <cstring>
  8. #include <string>
  9. #include <vector>
  10. #include <list>
  11. #include <queue>
  12. #include <stack>
  13. #include <map>
  14. #include <set>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. const int maxn = 1000 + 100;
  19. int n;
  20. int fib[maxn];
  21. int *it;
  22. int main() {
  23. #ifdef Dmaxiya
  24. freopen("test.txt", "r", stdin);
  25. #endif // Dmaxiya
  26. ios::sync_with_stdio(false);
  27. fib[1] = fib[2] = 1;
  28. for(int i = 3; i < 20; ++i) {
  29. fib[i] = fib[i - 1] + fib[i - 2];
  30. }
  31. while(scanf("%d", &n) != EOF) {
  32. for(int i = 1; i <= n; ++i) {
  33. it = lower_bound(fib, fib + 20, i);
  34. if(*it == i) {
  35. printf("O");
  36. } else {
  37. printf("o");
  38. }
  39. }
  40. printf("\n");
  41. }
  42. return 0;
  43. }

B. Radio Station

题意

每个 地址都有一个名字 ,总共有 ,有 条关于 的命令,现在要在每条命令后面加上 的名字并输出。

输入

第一行为两个整数 ,接下去 行,每行由一个字符串 和一个 地址组成, 地址的格式一定为 ,其中 且没有前导零,接下去 行每行由一条字符串 和一个 地址组成, 都只由小写字母组成。

输出

输出 行,每行为一条命令所对应的输出。

样例

输入 输出
2 2
main 192.168.0.2
replica 192.168.0.1
block 192.168.0.1;
proxy 192.168.0.2;
block 192.168.0.1; #replica
proxy 192.168.0.2; #main
3 5
google 8.8.8.8
codeforces 212.193.33.27
server 138.197.64.57
redirect 138.197.64.57;
block 8.8.8.8;
cf 212.193.33.27;
unblock 8.8.8.8;
check 138.197.64.57;
redirect 138.197.64.57; #server
block 8.8.8.8; #google
cf 212.193.33.27; #codeforces
unblock 8.8.8.8; #google
check 138.197.64.57; #server

题解

按题意写,用一个 存一下字符串之间的映射。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cfloat>
  7. #include <cstring>
  8. #include <string>
  9. #include <vector>
  10. #include <list>
  11. #include <queue>
  12. #include <stack>
  13. #include <map>
  14. #include <set>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. int n, m;
  19. string name, ip, command;
  20. map<string, string> mp;
  21. int main() {
  22. #ifdef Dmaxiya
  23. freopen("test.txt", "r", stdin);
  24. #endif // Dmaxiya
  25. ios::sync_with_stdio(false);
  26. while(cin >> n >> m) {
  27. mp.clear();
  28. for(int i = 0; i < n; ++i) {
  29. cin >> name >> ip;
  30. mp[ip] = name;
  31. }
  32. for(int i = 0; i < m; ++i) {
  33. cin >> command >> ip;
  34. cout << command << " " << ip << " #" << mp[ip.substr(0, ip.length() - 1)] << endl;
  35. }
  36. }
  37. return 0;
  38. }

C. The Monster

题意

给定一个只由 ()? 组成的字符串,问有多少个 区间,将这些区间中的所有 ?( 或者 ) 替换后,能够成为一个合法的括号序列。

输入

输入为一个只含有 ()? 的字符串

输出

输出所有满足条件的区间数量。

样例

输入 输出 提示
((?)) 4 "(?","?)","((?)","(?))" 这四个子串中的 ? 可以通过修改得到合法的括号序列。
??()?? 7 "()","??"(两个),"??()","?()?","()??","??()??" 这 个子串中的 ? 可以通过修改得到
合法的括号序列。

题解

扫描所有区间,对于每个区间, 地统计区间中左括号和右括号的配对情况(用一个变量 记录,遇到左括号则 ,遇到右括号则 ),以及问号的个数 ,则对于任意一个区间,如果出现 的情况,说明后面无论是什么样的序列都无法完成匹配,直接跳出循环,如果是 为偶数,则可能可以构成一个合法的序列,但是这样无法区分 ????((()(((????) 的情况(两种情况都满足前面的条件,第一个序列显然不合法而第二个序列显然合法),因此可以反过来从后往前再扫一遍整个字符串,取两次扫描合法区间的交集的个数,就是答案。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cfloat>
  7. #include <cstring>
  8. #include <string>
  9. #include <vector>
  10. #include <list>
  11. #include <queue>
  12. #include <stack>
  13. #include <map>
  14. #include <set>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. const int maxn = 5000 + 100;
  19. int num, tmp, len, ans;
  20. char str[maxn];
  21. int cnt[maxn][maxn];
  22. void solve(int Begin, int End, int d, char chl, char chr) {
  23. for(int i = Begin; i != End; i += d) {
  24. tmp = num = 0;
  25. for(int j = i; j != End; j += d) {
  26. if(str[j] == chl) {
  27. ++tmp;
  28. } else if(str[j] == chr) {
  29. --tmp;
  30. } else {
  31. ++num;
  32. }
  33. if(num + tmp < 0) {
  34. break;
  35. }
  36. if(num >= abs(tmp) && (num - abs(tmp)) % 2 == 0) {
  37. if(Begin < End) {
  38. ++cnt[i][j];
  39. } else {
  40. ++cnt[j][i];
  41. }
  42. }
  43. }
  44. }
  45. }
  46. int main() {
  47. #ifdef Dmaxiya
  48. freopen("test.txt", "r", stdin);
  49. #endif // Dmaxiya
  50. ios::sync_with_stdio(false);
  51. while(scanf("%s", str + 1) != EOF) {
  52. ans = 0;
  53. len = strlen(str + 1);
  54. for(int i = 1; i <= len; ++i) {
  55. memset(cnt[i], 0, sizeof(int) * (len + 1));
  56. }
  57. solve(1, len + 1, 1, '(', ')');
  58. solve(len, 0, -1, ')', '(');
  59. for(int i = 1; i <= len; ++i) {
  60. for(int j = i; j <= len; ++j) {
  61. if(cnt[i][j] == 2) {
  62. ++ans;
  63. }
  64. }
  65. }
  66. printf("%d\n", ans);
  67. }
  68. return 0;
  69. }

D. MADMAX

题意

两个人在一个 个节点的 上进行博弈游戏,每条边上都有一个小写字母,最初两个人分别在节点 和节点 上,由 开始两个人轮流进行移动,除了第一次移动,后面的每一次移动经过的边上的字母的 码都必须不小于前一次移动所经过的边上的字母,最终无法移动的人失败,在两人都采取最优策略的情况下,问谁将获胜。

输入

第一行为两个整数 ,接下来 行每行两个整数 以及一个小写字母 ,表示从 有一条有向边,边上的字母为

输出

输出一个 的字符矩阵,若最初 在第 个节点且 在第 个节点时,在双方都采取最优策略下 会获胜,则矩阵的第 行第 列为 ,否则为

样例

输入 输出 提示
4 4
1 2 b
1 3 a
2 4 c
3 4 b
BAAA
ABAA
BBBA
BBBB
地图如下:

5 8
5 3 h
1 2 c
3 1 c
3 2 r
5 1 r
4 3 z
5 4 r
5 2 h
BABBB
BBBBB
AABBB
AAABA
AAAAB
地图如下:

题解

记录状态 表示先手在第 个节点而后手在第 个节点,且当前先手走过的边上的字符必须不小于 时,先手必胜还是必败,最后输出 ,就是答案。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cfloat>
  7. #include <cstring>
  8. #include <string>
  9. #include <vector>
  10. #include <list>
  11. #include <queue>
  12. #include <stack>
  13. #include <map>
  14. #include <set>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. const int maxn = 100 + 100;
  19. struct Node {
  20. int pos;
  21. char ch;
  22. Node() {}
  23. Node(int p, char c) {
  24. pos = p;
  25. ch = c;
  26. }
  27. };
  28. int n, m, u, v;
  29. char ch[2];
  30. vector<Node> G[maxn];
  31. char str[maxn][maxn][26];
  32. int id(char ch) {
  33. return ch - 'a';
  34. }
  35. char dfs(int x, int y, char ch) {
  36. if(str[x][y][id(ch)] != '\0') {
  37. return str[x][y][id(ch)];
  38. }
  39. char ret = 'B';
  40. int len = G[x].size();
  41. for(int i = 0; i < len; ++i) {
  42. int pos = G[x][i].pos;
  43. char c = G[x][i].ch;
  44. if(c >= ch && dfs(y, pos, c) == 'B') {
  45. ret = 'A';
  46. break;
  47. }
  48. }
  49. str[x][y][id(ch)] = ret;
  50. return ret;
  51. }
  52. int main() {
  53. #ifdef Dmaxiya
  54. freopen("test.txt", "r", stdin);
  55. #endif // Dmaxiya
  56. ios::sync_with_stdio(false);
  57. while(scanf("%d%d", &n, &m) != EOF) {
  58. memset(str, 0, sizeof(str));
  59. for(int i = 1; i <= n; ++i) {
  60. G[i].clear();
  61. }
  62. for(int i = 0; i < m; ++i) {
  63. scanf("%d%d%s", &u, &v, ch);
  64. G[u].push_back(Node(v, ch[0]));
  65. }
  66. for(int i = 1; i <= n; ++i) {
  67. for(int j = 1; j <= n; ++j) {
  68. dfs(i, j, 'a');
  69. }
  70. }
  71. for(int i = 1; i <= n; ++i) {
  72. for(int j = 1; j <= n; ++j) {
  73. printf("%c", str[i][j][0]);
  74. }
  75. printf("\n");
  76. }
  77. }
  78. return 0;
  79. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注