Problem A Diagonal Walking

题目大意

  给定一个只包含'U'和'R'的字符串,你可以将"RU"或者'UR"替换为"D"。问能使串长能变成的最小值。

  直接把 RU 或者 UR 替换为 D 即可。

Code

/**
 * Codeforces
 * Problem#954A
 * Accepted
 * Time: 31ms
 * Memory: 0k
 */
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;

int n;
char s[233];

int main() {
	scanf("%d", &n);
	scanf("%s", s + 1);
	int ans = n;
	for (int i = 1; i < n; i++) {
		if (s[i] ^ s[i + 1]) {
			i++, ans--;
			continue;
		}
	}
	printf("%d\n", ans);
	return 0;
}

Problem B String Typing

题目大意

  有一台打字机,要输入1个字符串,有两种操作:

  1. 向末尾输入一个字符
  2. 将当前输入的字符串复制一遍粘在后面

  操作2只能使用1次。问最少的操作次数。

  没看到只能用1次,傻傻地写了个dp。并成功获得了:

Educational Codeforces Round 40 (Rated for Div. 2) Solution

  然后急急忙忙去加个状态。

  其实只能用1次直接贪心就好了。当减少的操作次数最多的时候用操作2.

Code

 1 /**
 2  * Codeforces
 3  * Problem#954B
 4  * Accepted
 5  * Time: 31ms
 6  * Memory: 0k
 7  */
 8 #include <bits/stdc++.h>
 9 using namespace std;
10 typedef bool boolean;
11 
12 int n;
13 char str[105];
14 int f[105][2];
15 
16 inline void init() {
17     scanf("%d", &n);
18     scanf("%s", str + 1);
19 }
20 
21 boolean equal(int l1, int l2, int len) {
22     for (int i = 0; i < len; i++)
23         if (str[l1 + i] != str[l2 + i])
24             return false;
25     return true;
26 }
27 
28 inline void solve() {
29     f[0][1] = 2000;
30     for (int i = 1; i <= n; i++) {
31         f[i][0] = f[i - 1][0] + 1;
32         f[i][1] = f[i - 1][1] + 1;
33         if (!(i & 1) && (equal(1, (i >> 1) + 1, i >> 1)))
34             f[i][1] = min(f[i][1], f[i >> 1][0] + 1);
35     }
36     printf("%d\n", min(f[n][0], f[n][1]));
37 }
38 
39 int main() {
40     init();
41     solve();
42     return 0;
43 }
Problem B

相关文章:

  • 2021-09-25
  • 2021-07-11
  • 2022-03-02
  • 2022-01-01
  • 2021-05-17
猜你喜欢
  • 2022-12-23
  • 2021-06-05
  • 2021-10-25
  • 2022-12-23
  • 2021-05-25
  • 2021-12-23
  • 2021-05-17
相关资源
相似解决方案