您的位置:首页 > 其它

【hihocoder1039】字符消除——模拟

2016-08-24 17:17 316 查看

题目: 点击进入

描述:

一个类似祖玛的字符串游戏,只有ABC三种字母,要求添加一个字母,使最后消除的字母最多,字符串长度不超过100.

题解:

枚举每一个位置分别加入三种字母进行消除,ans记录最大结果。

枚举是要<=len,因为可以加入的位置是

第一个字符之前、最后一个字符之后以及相邻两个字符之间

代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <climits>
#include <cassert>
#include <cctype>
#include <complex>
#include <algorithm>
#include <string>
#include <iostream>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <set>
using namespace std;
#define fi first
#define se second
#define MP(A, B) make_pair(A, B)
#define pb push_back
#define gcd __gcd
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
typedef long long ll;
typedef unsigned long long ulls;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<pii, int> mpi;
#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
#define lld %I64d
#else
#define lld %lld
#endif
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-6;
const int maxn = 1e2 + 5;
const int maxm = 1e6 + 5;
const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
inline int scan(int &a) { return scanf("%d", &a); }
inline int scan(int &a, int &b) { return scanf("%d%d", &a, &b); }
inline int scan(int &a, int &b, int &c) { return scanf("%d%d%d", &a, &b, &c); }
inline int scan(ll &a) { return scanf("lld", &a); }
inline int scan(ll &a, ll &b) { return scanf("lldlld", &a, &b); }
inline int scan(ll &a, ll &b, ll &c) { return scanf("lldlldlld", &a, &b, &c); }
inline int scan(double &a) { return scanf("%lf", &a); }
inline int scan(double &a, double &b) { return scanf("%lf%lf", &a, &b); }
inline int scan(double &a, double &b, double &c) { return scanf("%lf%lf%lf", &a, &b, &c); }
inline int scan(char &a) { return scanf("%c", &a); }
inline int scan(char *a) { return scanf("%s", a); }
template<class T> inline void mem(T &A, int x) { memset(A, x, sizeof(A)); }
template<class T0, class T1> inline void mem(T0 &A0, T1 &A1, int x) { mem(A0, x), mem(A1, x); }
template<class T0, class T1, class T2> inline void mem(T0 &A0, T1 &A1, T2 &A2, int x) { mem(A0, x), mem(A1, x), mem(A2, x); }
template<class T0, class T1, class T2, class T3> inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) { mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x); }
template<class T0, class T1, class T2, class T3, class T4> inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) { mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x), mem(A4, x); }
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) { mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x), mem(A4, x), mem(A5, x); }
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) { mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x), mem(A4, x), mem(A5, x), mem(A6, x); }
template<class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template<class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); }
template<class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c, T d, T e) { return min(min(min(a,b),min(c,d)),e); }
template<class T> inline T max(T a, T b, T c, T d, T e) { return max(max(max(a,b),max(c,d)),e); }
string s;
int del(string p)
{
int len = p.size(), rest;
if(len == 0) return 0;
string tmp = "";
p += "D";
int flag = 0;
for(int i = 1; i <= len; i++)
{
if(p[i] != p[i - 1])
{
if(flag == i - 1) tmp += p[i - 1];
flag = i;
}
}
if((rest = tmp.size()) == len) return 0;
return len - rest + del(tmp);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif

int T; scan(T);
while(T--)
{
cin >> s; string tmp = "";
int ans = 0;
for(int i = 0, len = s.size(); i <= len; i++)
{
tmp = s;
tmp.insert(i, "A");
ans = max(ans, del(tmp));
tmp = s;
tmp.insert(i, "B");
ans = max(ans, del(tmp));
tmp = s;
tmp.insert(i, "C");
ans = max(ans, del(tmp));
}
printf("%d\n", ans);
}

#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms\n", _end_time - _begin_time);
#endif

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: