题目的意思就是在一个有12个位置的地方放了一些石头..
你可以拿走石头,但是条件是,石头必须是这样的 -OO 或者OO-就是连续三个位置,开抖没有石头,剩下都有,或者最后没有石头,剩下都有.
然后你可以把最后的石头移到第一个,并把中间的石头拿走..
问拿完能剩几个...
做法就是我们要计算这种情况可以拿走几个,,
因为只有12位.所以我们可以用哈希表示每一种状态,但是我这里用了map();
比如现在是字符串s
那么f[ s ] ,也就是这种状态下能移走几个,,,,就是 MAx ( f [ s ] , f[ t ] +1) 而t是s经过一次移动能得到的状态..
AC代码:
#include<stdio.h>
#include<iostream>
#include<string>
#include<map>
using namespace std;
const int N = 15;
string s;
map<string , int> f;
int dp(string s) {
string temp;
if(f[s] != 0) {
return f[s];
}
// f[s] = 1;
for (int i = 0 ; i < 10 ;i++) {
if (s[i] == 'o' && s[i + 1] == 'o' && s[i + 2] == '-') {
temp = s;
temp[i] = '-';
temp[i + 1] = '-';
temp[i + 2] = 'o';
int flag = dp(temp);
f[s] = f[s] > flag + 1 ? f[s] : flag + 1;
}
}
for (int i = 0; i < 10 ;i++) {
if (s[i] == '-' && s[i + 1] == 'o' && s[i + 2] == 'o') {
temp = s;
temp[i] = 'o';
temp[i + 1] = '-';
temp[i + 2] = '-';
int flag = dp(temp);
f[s] = f[s] > flag + 1 ? f[s] : flag + 1;
}
}
return f[s];
}
int main () {
int t;
f.clear();
scanf("%d",&t);
while (t--) {
int res = 0;
cin >> s;
for (int i = 0 ; i < 12 ;i++) {
if (s[i] == 'o')
res++;
}
cout << res - dp(s) << endl;
}
}