[关闭]
@Dmaxiya 2018-08-17T10:28:37.000000Z 字数 4443 阅读 1276

Codeforces Round #455 (Div. 2)

Codeforces


Contests 链接:Codeforces Round #455 (Div. 2)
过题数:4
排名:225/7758

A. Generate Login

题意

给出一个人的姓和名,分别用小写字符串 表示,一个人的登陆用户名为两个字符串的前缀拼起来,要求这个用户名的字典序最小,求这个用户名。

数据范围

题解

按照题意,取两个字符串的前缀拼起来放到 里面,然后取 指向的元素。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. string str1, str2;
  21. string str;
  22. set<string> st;
  23. int main() {
  24. #ifdef LOCAL
  25. freopen("test.txt", "r", stdin);
  26. // freopen("out.txt", "w", stdout);
  27. #endif // LOCAL
  28. ios::sync_with_stdio(false);
  29. while(cin >> str1 >> str2) {
  30. st.clear();
  31. int len1 = str1.length();
  32. int len2 = str2.length();
  33. for(int i = 1; i <= len1; ++i) {
  34. for(int j = 1; j <= len2; ++j) {
  35. str = str1.substr(0, i) + str2.substr(0, j);
  36. st.insert(str);
  37. }
  38. }
  39. set<string>::iterator it = st.begin();
  40. cout << *it << endl;
  41. }
  42. return 0;
  43. }

B. Segments

题意

给一个长度为 的区间,这个区间上左右端点为整数的线段总共有 条,现在将这些线段不移动左右端点地平铺在这个区间上,问最少铺多少层。

数据范围

题解

将每一条线段都取一遍,按照线段覆盖问题处理。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 100 + 100;
  21. int n;
  22. int num[maxn];
  23. int main() {
  24. #ifdef LOCAL
  25. freopen("test.txt", "r", stdin);
  26. // freopen("out.txt", "w", stdout);
  27. #endif // LOCAL
  28. ios::sync_with_stdio(false);
  29. while(scanf("%d", &n) != EOF) {
  30. memset(num, 0, sizeof(num));
  31. for(int i = 0; i < n; ++i) {
  32. for(int j = i + 1; j <= n; ++j) {
  33. ++num[i];
  34. --num[j];
  35. }
  36. }
  37. int ans = 0;
  38. int tmp = 0;
  39. for(int i = 0; i <= n; ++i) {
  40. tmp += num[i];
  41. ans = max(ans, tmp);
  42. }
  43. printf("%d\n", ans);
  44. }
  45. return 0;
  46. }

C. Python Indentation

题意

语法靠缩进来确定程序的逻辑,现在给出 条语句,总共只有两种语法,一种是简单句,单独占一行,另一种是 语句, 语句下可以包含多个语句, 语句可以嵌套使用, 语句的下一行必须有一个缩进, 语句内不能为空,即必须要有循环体。现在给出每一条语句为 语句)或者 (简单句),但没有缩进,问总共有多少种可能的合法的 语句。最后将求得的方案数对 取模。

数据范围

题解

定义 表示到第 行语句,这条语句在第 个缩进的可能性的多少,将第 条语句记为 ,以此可以推导出状态转移方程:


方程可以看出,每一层只要从后往前循环,就可以将二维拍成一维来写,对于求和操作,就用一个 数组来记录,时间复杂度为

过题代码

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

D. Colorful Points

题意

有一个长度为 的小写字母序列,对这个序列进行多次操作,每次操作先确定所有旁边有不同字母的位置,然后将这些位置上的字母删掉,问这个序列最多经过多少次操作后,无法进行操作。

数据范围

题解

先对整个序列扫一遍,记录下每一段的长度和字母,然后按照题意模拟一遍,时间复杂度为

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 1000000 + 100;
  21. struct Node {
  22. int len;
  23. char ch;
  24. };
  25. char str[maxn];
  26. Node node[maxn];
  27. int main() {
  28. #ifdef LOCAL
  29. freopen("test.txt", "r", stdin);
  30. // freopen("out.txt", "w", stdout);
  31. #endif // LOCAL
  32. ios::sync_with_stdio(false);
  33. while(scanf("%s", str) != EOF) {
  34. int cnt = 0;
  35. node[cnt].len = 0;
  36. node[cnt].ch = str[0];
  37. for(int i = 1; str[i]; ++i) {
  38. if(str[i] != str[i - 1]) {
  39. node[cnt].len = i - node[cnt].len;
  40. node[++cnt].len = i;
  41. node[cnt].ch = str[i];
  42. }
  43. }
  44. node[cnt].len = strlen(str) - node[cnt].len;
  45. ++cnt;
  46. int ans = 0;
  47. while(cnt > 1) {
  48. --node[0].len;
  49. --node[cnt - 1].len;
  50. for(int i = 1; i < cnt - 1; ++i) {
  51. node[i].len -= 2;
  52. }
  53. int cnttmp = 0;
  54. for(int i = 0; i < cnt; ++i) {
  55. if(node[i].len > 0) {
  56. node[cnttmp++] = node[i];
  57. }
  58. }
  59. cnt = cnttmp;
  60. cnttmp = 0;
  61. for(int i = 1; i < cnt; ++i) {
  62. if(node[i].ch == node[cnttmp].ch) {
  63. node[cnttmp].len += node[i].len;
  64. } else {
  65. ++cnttmp;
  66. node[cnttmp] = node[i];
  67. }
  68. }
  69. cnt = cnttmp + 1;
  70. ++ans;
  71. }
  72. printf("%d\n", ans);
  73. }
  74. return 0;
  75. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注