[HIHO1039]字符消除(字符串,枚举,模拟)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[HIHO1039]字符消除(字符串,枚举,模拟)相关的知识,希望对你有一定的参考价值。
题目链接:http://hihocoder.com/problemset/problem/1039
思路:枚举所有字符更新的位置和ABC三种修改方案,之后再模拟消除规则,一步一步去消除。直到无法消除,用原串长度减去当前串长度,更新答案。竟然写了好久。
1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <fstream> 24 #include <cassert> 25 #include <cstdio> 26 #include <bitset> 27 #include <vector> 28 #include <deque> 29 #include <queue> 30 #include <stack> 31 #include <ctime> 32 #include <set> 33 #include <map> 34 #include <cmath> 35 using namespace std; 36 #define fr first 37 #define sc second 38 #define cl clear 39 #define BUG puts("here!!!") 40 #define W(a) while(a--) 41 #define pb(a) push_back(a) 42 #define Rint(a) scanf("%d", &a) 43 #define Rll(a) scanf("%lld", &a) 44 #define Rs(a) scanf("%s", a) 45 #define Cin(a) cin >> a 46 #define FRead() freopen("in", "r", stdin) 47 #define FWrite() freopen("out", "w", stdout) 48 #define Rep(i, len) for(int i = 0; i < (len); i++) 49 #define For(i, a, len) for(int i = (a); i < (len); i++) 50 #define Cls(a) memset((a), 0, sizeof(a)) 51 #define Clr(a, x) memset((a), (x), sizeof(a)) 52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 53 #define lrt rt << 1 54 #define rrt rt << 1 | 1 55 #define pi 3.14159265359 56 #define RT return 57 #define lowbit(x) x & (-x) 58 #define onenum(x) __builtin_popcount(x) 59 typedef long long LL; 60 typedef long double LD; 61 typedef unsigned long long ULL; 62 typedef pair<int, int> pii; 63 typedef pair<string, int> psi; 64 typedef pair<LL, LL> pll; 65 typedef map<string, int> msi; 66 typedef vector<int> vi; 67 typedef vector<LL> vl; 68 typedef vector<vl> vvl; 69 typedef vector<bool> vb; 70 71 const int maxn = 110; 72 string s, t, f; 73 bool vis[maxn]; 74 int n, m, k; 75 int ret; 76 77 int check(int p, char c) { 78 t.cl(); f.cl(); 79 if(p == 0) { 80 t.pb(c); 81 Rep(i, n) t.pb(s[i]); 82 } 83 else if(p == n) { 84 t = s; 85 t.pb(c); 86 } 87 else { 88 Rep(i, p) t.pb(s[i]); 89 t.pb(c); 90 For(i, p, n) t.pb(s[i]); 91 } 92 while(1) { 93 bool exflag = 1; 94 m = t.length(); 95 f.cl(); Cls(vis); 96 Rep(i, m-1) { 97 if(t[i] == t[i+1]) { 98 vis[i] = 1; 99 vis[i+1] = 1; 100 exflag = 0; 101 } 102 } 103 Rep(i, m) { 104 if(!vis[i]) { 105 f.pb(t[i]); 106 } 107 } 108 t = f; 109 if(exflag) break; 110 } 111 return n + 1 - m; 112 } 113 114 int main() { 115 // FRead(); 116 int T; 117 Rint(T); 118 W(T) { 119 cin >> s; 120 n = s.length(); 121 ret = 0; 122 For(i, 0, n+1) { 123 ret = max(ret, check(i, ‘A‘)); 124 ret = max(ret, check(i, ‘B‘)); 125 ret = max(ret, check(i, ‘C‘)); 126 } 127 printf("%d\n", ret); 128 } 129 RT 0; 130 }
以上是关于[HIHO1039]字符消除(字符串,枚举,模拟)的主要内容,如果未能解决你的问题,请参考以下文章