题目大意:
就是现在给出一个长度为N(N <= 800)的序列, 只包含字母'R', 'G', '?'现在可以对'?'处的字符填入'R'或者'G', 问在所有的填法当中, 是beautiful的方案的有哪些, beautiful的方案值得是至少存在一组数a, b, c成等差数列使得第a, c为都是R并且b位时'G', 可行方案数对1e9 + 7取模
大致思路:
这个题就是个思路题, 感觉很巧妙
由于正面考虑不容易计算, 我们从反面考虑不是beautiful的方案有多少然后用总方案数减去即可
如果不是beautiful的方案, 那么对于任意两个'R', 其中间位置都不能是'G'
仔细观察会发现, 这样的序列只可能出现以下几种可能:
1. 序列中根本没有'R', 或者有1个'R'
2. 序列中有多个R, 且相邻R之间相隔的位置必定是偶数个, 如果出现了RxxxR的情况中间一定有一个'G'使得这个序列是beautiful的, 另外还有一个要求就是这些相邻的R之间必定等距离, 试想, 有这样的序列: R1 ___ R2 ________R3, 间隔分别是2*k1, 2*k2, 那么R1和R3之间会间隔2*(k1 + k2) + 1个位置, 而R2并不在中间, 那么'G'就会变成两个'R'的中心, 这样会使得这个序列成为beautiful的方案
通过上面的结论可以发现, 只需要枚举R的的起始位置和间距即可
枚举起始位置O(n), 对于多个间距得到的序列, 枚举间距复杂度为O(n/1 + n/2 + n /4 + ... + n/2*k) = O(nlogn)
那么总体复杂度是O(n*n*logn)
代码如下:
Result : Accepted Memory : 1604 KB Time : 78 ms
/*
* Author: Gatevin
* Created Time: 2015/8/3 15:08:49
* File Name: Sakura_Chiyo.cpp
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
#define maxn 888
char s[maxn];
const int mod = 1e9 + 7;
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%s", s);
int len = strlen(s);
int tot = 1;
int cntR = 0;
for(int i = 0; i < len; i++)
if(s[i] == 'R') cntR++;
else if(s[i] == '?') tot <<= 1, tot %= mod;
int bad = 0;
if(cntR == 0) bad = 1;//一个R都没有
for(int r = 0; r < len; r++)//枚举'R'的起始位置
{
int R = cntR;
if(s[r] == 'R') R--;
if(s[r] == 'G') continue;
if(R == 0) bad++;
for(int d = 0; r + d + 1 < len; d += 2)//多个R, 枚举R之间的间隔空位个数
{
int tR = R;
int k = r + d + 1;
while(k < len)//当前从r到k间隔d都是R, 其他不是
{
if(s[k] == 'R') tR--;
if(s[k] == 'G') break;//这种方式走不下去, 违反初始的'G'
if(tR == 0) bad++;
k += d + 1;
}
}
}
printf("%d\n", (tot - bad + mod) % mod);
}
return 0;
}