hihoCoder#1039 字符消除

原题地址

 

注意题目,连续字符同时被消除,所以消除无顺序区别。

纯模拟题,数据量很小,所以暴力做就行了:枚举所有可能插入的位置,每个位置再枚举插入字符

想了几个优化的方案,但是估计是哪里错了,导致得出的结果是WA。后来只好乖乖用最暴力的方法AC。

 

代码:

 1 #include <iostream>
 2 #include <cstring>
 3 
 4 using namespace std;
 5 
 6 int get_score(char *s) {
 7   int score = 0;
 8   bool over = false;
 9 
10   over = false;
11   while (!over) {
12     int i = 0;
13     int j = 0;
14 
15     over = true;
16     while (s[j]) {
17       if (s[j] == s[j + 1]) {
18         int c = s[j];
19         while (s[j] == c) {
20           j++;
21           score++;
22         }
23         over = false;
24       }
25       else
26         s[i++] = s[j++];
27     }
28     s[i] = 0;
29   }
30 
31   return score;
32 }
33 
34 int main() {
35   int n = 0;
36 
37   cin >> n;
38   while (n--) {
39     char s[128] = {0};
40     char t[128] = {0};
41     int score = 0;
42     int len = 0;
43 
44     cin >> s;
45     len = strlen(s);
46 
47     for (int i = 0; i <= len; i++) {
48       for (int j = 0; j < 3; j++) {
49         memcpy(t, s, i);
50         memcpy(t + i + 1, s + i, len - i);
51         t[len + 1] = 0;
52         t[i] = 'A' + j;
53         score = max(score, get_score(t));
54       }
55     }
56     cout << score << endl;
57   }
58   return 0;
59 }

 

posted @ 2015-03-21 17:12  李舜阳  阅读(375)  评论(0编辑  收藏  举报