[关闭]
@Dmaxiya 2018-08-18T21:06:27.000000Z 字数 7522 阅读 1078

Codeforces Round #504 (Div. 1 + Div. 2)

Codeforces


Contests 链接:Codeforces Round #504 (Div. 1 + Div. 2)
过题数:3
排名:1013/7459

A. Single Wildcard Pattern Matching

题意

给定两个字符串 ,长度分别为 串中最多含有一个 * 通配符,通配符可以用任意字符串(也可能为空)替换,问能否将其中的通配符用某个字符串替换后,使 串相等。

输入

第一行为两个整数 ,第二行为一个长度为 的字符串 ,第三行为一个长度为 的字符串 串中只含有小写字母与最多一个 * 字符, 串只包含小写字母。

输出

如果 串可以转化为 串,则输出 ,否则输出

样例

输入
6 10
code*s
codeforces
输出
YES
提示
* 改为 force 就可以令两个字符串相等。
输入
6 5
vk*cup
vkcup
输出
YES
提示
用空串代替 * 就可以令两个字符串相等。
输入
1 1
v
k
输出
NO
提示
两个字符串不相等,故答案为
输入
9 6
gfgf*gfgf
gfgfgf
输出
NO
提示
不论用什么字符串替换 * 都无法使两个字符串相等。

题解

如果 串不包含通配符,直接用 比较,否则从前往后、从后往前与 串进行比较,记录 串的分隔位,如果 串的 * 之前与 的公共前缀与 * 之后与 的公共后缀不相交,就输出 ,否则输出

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <climits>
  6. #include <cmath>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <functional>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. const int maxn = 200000 + 100;
  19. int n, m;
  20. char s[maxn], t[maxn];
  21. int main() {
  22. #ifdef Dmaxiya
  23. freopen("test.txt", "r", stdin);
  24. // freopen("10.out", "w", stdout);
  25. #endif // Dmaxiya
  26. while(scanf("%d%d", &n, &m) != EOF) {
  27. int l, r;
  28. int tl, tr;
  29. bool flag = false;
  30. scanf("%s%s", s, t);
  31. for(int i = 0; i < n; ++i) {
  32. if(s[i] == '*') {
  33. flag = true;
  34. l = i - 1;
  35. r = i + 1;
  36. }
  37. }
  38. if(!flag) {
  39. if(strcmp(s, t) == 0) {
  40. printf("YES\n");
  41. } else {
  42. printf("NO\n");
  43. }
  44. continue;
  45. }
  46. tl = -1;
  47. tr = m;
  48. for(int i = 0; i <= l; ++i) {
  49. if(s[i] != t[i]) {
  50. flag = false;
  51. break;
  52. }
  53. tl = i;
  54. }
  55. for(int i = 1; n - i >= r; ++i) {
  56. if(s[n - i] != t[m - i]) {
  57. flag = false;
  58. break;
  59. }
  60. tr = m - i;
  61. }
  62. if(flag) {
  63. if(tl < tr) {
  64. printf("YES\n");
  65. } else {
  66. printf("NO\n");
  67. }
  68. } else {
  69. printf("NO\n");
  70. }
  71. }
  72. return 0;
  73. }

B. Pair of Toys

题意

个玩具,第 个玩具的价格为 ,问有多少对玩具 ,它们的价值和等于 ,其中 被认为是相同的玩具对。

输入

输入只包含两个整数

输出

输出满足条件的玩具对数。

样例

输入
8 5
输出
2
提示
都是合法的选择。
输入
8 15
输出
1
提示
只有 是合法的选择。
输入
7 20
输出
0
提示
中所有数字相加都不能达到 ,因此答案为
输入
1000000000000 1000000000001
输出
500000000000
提示
我们可以选择 ,总共有 对整数。

题解

先确定所有可选数字的右边界 ,再确定其左边界 ,其中满足条件的整数对数为

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <climits>
  6. #include <cmath>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <functional>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. LL n, k;
  19. int main() {
  20. #ifdef Dmaxiya
  21. freopen("test.txt", "r", stdin);
  22. // freopen("10.out", "w", stdout);
  23. #endif // Dmaxiya
  24. while(scanf("%I64d%I64d", &n, &k) != EOF) {
  25. LL R = min(k - 1, n);
  26. LL L = max(1LL, k - R);
  27. if(L >= R) {
  28. printf("0\n");
  29. continue;
  30. }
  31. printf("%I64d\n", (R - L + 1) / 2);
  32. }
  33. return 0;
  34. }

C. Bracket Subsequence

题意

给定一个长度为 的合法括号序列,其中的左右括号能完全匹配,要求从中找到一个长度为 的合法括号子序列。

输入

第一行为两个整数 ,其中 都是偶数,第二行为一个长度为 的字符串,字符串保证是一个合法的括号序列。

输出

输出长度为 的合法括号子序列。

样例

输入
6 4
()(())
输出
()()
输入
8 8
(()(()))
输出
(()(()))

题解

用栈模拟括号匹配的过程,其中弹出栈的必然是合法的括号序列,将弹出栈的括号下标标记,当弹出栈的括号个数达到 时,将所有标记的括号输出。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <climits>
  6. #include <cmath>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <functional>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. const int maxn = 200000 + 100;
  19. int n, k, top;
  20. char str[maxn];
  21. bool vis[maxn];
  22. int sta[maxn];
  23. int main() {
  24. #ifdef Dmaxiya
  25. freopen("test.txt", "r", stdin);
  26. // freopen("10.out", "w", stdout);
  27. #endif // Dmaxiya
  28. while(scanf("%d%d", &n, &k) != EOF) {
  29. top = 0;
  30. memset(vis, 0, sizeof(vis));
  31. scanf("%s", str);
  32. for(int i = 0; i < n; ++i) {
  33. if(str[i] == '(') {
  34. sta[top++] = i;
  35. } else {
  36. --top;
  37. vis[sta[top]] = true;
  38. vis[i] = true;
  39. k -= 2;
  40. if(k == 0) {
  41. break;
  42. }
  43. }
  44. }
  45. for(int i = 0; i < n; ++i) {
  46. if(vis[i]) {
  47. printf("%c", str[i]);
  48. }
  49. }
  50. printf("\n");
  51. }
  52. return 0;
  53. }

D. Array Restoration

题意

有一个长度为 的序列,可以对这 个序列进行 次操作,第 次操作选择一个区间 ,将这个区间内的所有数字用 替换,每个位置上的数字至少被一个区间覆盖,最后将这个序列中的某几个数字改为 。现在题目给出最终的序列,问是否存在合法的 次操作,能够得到给出的序列,求 次操作后(将序列某些数字变为 之前)的序列。

输入

第一行为两个整数 ,第二行为 个整数

输出

如果给出的序列无法从上面的操作中得到,输出 ,否则在第一行输出 ,第二行输出 次操作之后的 个整数。

样例

输入
4 3
1 0 2 3
输出
YES
1 2 2 3
提示
替换也是合法的,但是用 替换是不合法的。
输入
3 10
10 10 10
输出
YES
10 10 10
提示
不论前 次操作是怎么样,第 次操作是选择区间 即可。
输入
5 6
6 5 6 2 2
输出
NO
提示
由于第 次操作必须在第 次操作之前,所以不可能先将区间 更新为 之后再将区间 更新为
输入
3 5
0 0 0
输出
YES
5 4 2
提示
有许多种操作对于 而言都是合法的。

题解

由于数字大的一定比数字小的后更新,所以每个数字出现的左端点与右端点之间的数字不能小于它自身,可以用线段树维护最小值,从 询问第 个数字出现的左端点 与右端点 ,如果在查询区间最小值时遇到 的情况,就向下将 更新为当前数字再进行查询。记得首先查询序列内是否有数字 ,如果没有数字 就将其中任意一个 修改为 ,如果无法修改则直接输出 。所有询问与更新结束后,如果序列中仍含有 ,就将所有 赋值为

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <climits>
  6. #include <cmath>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <functional>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. const int maxn = 200000 + 100;
  19. int n, q;
  20. int num[maxn], L[maxn], R[maxn];
  21. int Min[maxn << 2];
  22. void push_up(int rt) {
  23. Min[rt] = min(Min[rt << 1], Min[rt << 1 | 1]);
  24. }
  25. void build(int l, int r, int rt) {
  26. if(l == r) {
  27. Min[rt] = num[l];
  28. return ;
  29. }
  30. int m = (l + r) >> 1;
  31. build(l, m, rt << 1);
  32. build(m + 1, r, rt << 1 | 1);
  33. push_up(rt);
  34. }
  35. void update(int x, int l, int r, int rt) {
  36. if(l == r) {
  37. Min[rt] = x;
  38. num[l] = x;
  39. return ;
  40. }
  41. int m = (l + r) >> 1;
  42. if(Min[rt << 1] == 0) {
  43. update(x, l, m, rt << 1);
  44. }
  45. if(Min[rt << 1 | 1] == 0) {
  46. update(x, m + 1, r, rt << 1 | 1);
  47. }
  48. push_up(rt);
  49. }
  50. int query(int L, int R, int x, int l, int r, int rt) {
  51. if(L <= l && r <= R) {
  52. if(Min[rt] == 0) {
  53. update(x, l, r, rt);
  54. }
  55. return Min[rt];
  56. }
  57. int m = (l + r) >> 1;
  58. int ret = INT_MAX;
  59. if(L <= m) {
  60. ret = min(ret, query(L, R, x, l, m, rt << 1));
  61. }
  62. if(m < R) {
  63. ret = min(ret, query(L, R, x, m + 1, r, rt << 1 | 1));
  64. }
  65. push_up(rt);
  66. return ret;
  67. }
  68. int main() {
  69. #ifdef Dmaxiya
  70. freopen("test.txt", "r", stdin);
  71. // freopen("10.out", "w", stdout);
  72. #endif // Dmaxiya
  73. while(scanf("%d%d", &n, &q) != EOF) {
  74. for(int i = 0; i <= q; ++i) {
  75. L[i] = n + 1;
  76. R[i] = 0;
  77. }
  78. for(int i = 1; i <= n; ++i) {
  79. scanf("%d", &num[i]);
  80. L[num[i]] = min(L[num[i]], i);
  81. R[num[i]] = max(R[num[i]], i);
  82. }
  83. if(L[q] > R[q]) {
  84. if(L[0] != n + 1) {
  85. num[L[0]] = q;
  86. L[q] = R[q] = L[0];
  87. } else {
  88. printf("NO\n");
  89. continue;
  90. }
  91. }
  92. build(1, n, 1);
  93. bool flag = true;
  94. for(int i = q; i >= 1; --i) {
  95. if(L[i] > R[i]) {
  96. continue;
  97. }
  98. if(query(L[i], R[i], i, 1, n, 1) < i) {
  99. flag = false;
  100. break;
  101. }
  102. }
  103. if(!flag) {
  104. printf("NO\n");
  105. continue;
  106. }
  107. printf("YES\n");
  108. for(int i = 1; i <= n; ++i) {
  109. if(i != 1) {
  110. printf(" ");
  111. }
  112. if(num[i] == 0) {
  113. printf("1");
  114. } else {
  115. printf("%d", num[i]);
  116. }
  117. }
  118. printf("\n");
  119. }
  120. return 0;
  121. }

E. Down or Right

题意

这是一道交互题,有一个 的网格,其中有一些网格是可以行走的,而有一些是无法行走的,在可以行走的网格中,下一步只能往右或下两个方向移动到下一格可以行走的网格中,但是我们并不知道这个网格哪些格子是可以行走的那些无法行走。 要从 点走到 点,每次可以询问他从 点是否能到达 点, 将会回答 或者 ,且每次询问 的哈密顿距离不能小于 ,最终输出从 的合法行走方案。询问的次数不能超过

输入

第一次输入为一个整数 ,后面的输入为 或者 ,取决于输出的询问。

输出

如果为询问,则按照 的格式询问,如果已经可以求得路径,则第一个字符为 !,空一格之后,输出 个字符,第 个字符为 D 表示第 步需要往下走,为 R 表示需要往右走。

样例

输入
4

YES

NO

YES

YES
输出

? 1 1 4 4

? 1 2 4 3

? 4 1 4 4

? 1 4 4 4

! RDRRDD
提示
网格中的可行走方格与不可行走方格如下图:

题解

如果“左上的点”表示与点 的哈密顿距离不大于 的所有点,“右下的点”表示与点 的哈密顿距离不大于 的点,则先从 开始询问左上的点是否能到达 ,能往右就尽量往右走,再从 开始询问 能否到达右下的点,能往上就尽量往上,最后这两条路径一定会重合于某一点 ,其中

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <climits>
  6. #include <cmath>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <functional>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. const int maxn = 20000 + 100;
  19. int n, cntl, cntr;
  20. char ret[10];
  21. char ansl[maxn], ansr[maxn];
  22. int dis(int x1, int y1, int x2, int y2) {
  23. return abs(x1 - x2) + abs(y1 - y2);
  24. }
  25. int main() {
  26. #ifdef Dmaxiya
  27. // freopen("test.txt", "r", stdin);
  28. // freopen("10.out", "w", stdout);
  29. #endif // Dmaxiya
  30. scanf("%d", &n);
  31. int x = 1, y = 1;
  32. while(dis(1, 1, x, y + 1) <= n - 1) {
  33. int xx = x;
  34. int yy = y + 1;
  35. printf("? %d %d %d %d\n", xx, yy, n, n);
  36. fflush(stdout);
  37. scanf("%s", ret);
  38. if(ret[0] == 'Y') {
  39. ansl[cntl++] = 'R';
  40. ++y;
  41. } else {
  42. ansl[cntl++] = 'D';
  43. ++x;
  44. }
  45. }
  46. x = y = n;
  47. while(dis(x - 1, y, n, n) <= n - 1) {
  48. int xx = x - 1;
  49. int yy = y;
  50. printf("? %d %d %d %d\n", 1, 1, xx, yy);
  51. fflush(stdout);
  52. scanf("%s", ret);
  53. if(ret[0] == 'Y') {
  54. ansr[cntr++] = 'D';
  55. --x;
  56. } else {
  57. ansr[cntr++] = 'R';
  58. --y;
  59. }
  60. }
  61. ansl[cntl] = '\0';
  62. printf("! %s", ansl);
  63. for(int i = cntr - 1; i >= 0; --i) {
  64. printf("%c", ansr[i]);
  65. }
  66. printf("\n");
  67. fflush(stdout);
  68. return 0;
  69. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注