hdu 4346 The Beautiful Road(思维,枚举,5级)

来源:互联网 发布:全景摄影师 知乎 编辑:程序博客网 时间:2024/05/16 12:57

The Beautiful Road

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 229    Accepted Submission(s): 122


Problem Description
There is a road from city A to city B.On the road,there are N positions(indexed from 0 to N-1).In order to celebrate the Multi-University training,the mayor of city A commands a worker to insert N flags on the N positions.The color of a flag is either red or green.We call the road is beautiful if and only if there exists at least one pair of non-negative integers (a,b) (0<=a<b<N and (a+b) is an even number) such that both a-th flag and b-th flag are red and (a+b)/2-th flag is green.Otherwise we call the road is ugly.Now,some positions have already been inserted flags.In order to make the road beautiful,the worker must carefully insert the flags on the positions which haven't been inserted.He wants to know how many different types of beautiful road he can make.The type of the road only depends on the flags' color.
 

Input
The first line of the input is the number of cases. On each case there is a line consists of a string.The length of the string is N.(0<=N<=800).The i-th character of the string is 'R' or 'G' or '?'.'R' means the flag on the i-th position which has been inserted is red.'G' means the flag on the i-th position which has been inserted is green.'?' means the i-th position hasn't been inserted a flag.
 

Output
A single line with the number of different types of road the worker can make,modulo 1,000,000,007.
 

Sample Input
4?GRG????????
 

Sample Output
0114
 

Author
HIT
 

Source
2012 Multi-University Training Contest 5
 

Recommend
zhuyuanchen520


思路:beautiful串数=总串数-非butiful串数

         而非beautiful串的形式必然为:G..GR...R...R...RG..G,相邻2R的距离相等且距离为奇

     证明不难,反证法,假设其中有一对相邻距离为偶数,则必能找到中间的G

    假设相邻的R距离不等,则此边界的R符合条件,并且能找到G。

剩下的枚举R的可能起点,和R间的距离,这题就水了。


#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int mm=808;const long long mod=1000000007;int cas,R_all,R,num;long long ans;char s[mm];int main(){    while(~scanf("%d",&cas))    {        while(cas--)        {            num=R=R_all=0;            scanf("%s",s);            for(int i=0; s[i]; ++i)            {                num+=(s[i]=='?');                R_all+=(s[i]=='R');            }            ans=0;            int len=strlen(s);            if(R_all==0)++ans;///全为G的情况            bool flag;            for(int i=0; i<len; ++i)            {                flag=(s[i]=='R');///起点为R碰到后计算一次就出现所有结果了                if(s[i]!='G')                {                    if(R_all==0)ans=(ans+1)%mod;///前i个都是G,i为R,后i位是G,                    if(R_all==1&&s[i]=='R')ans=(ans+1)%mod;///前面全是G,i位后面全是G,                    for(int j=1; j+i<len; j+=2) ///相邻R的最小距离                    {                        R=(s[i]=='R');                        for(int k=i+j; k<len; k+=j) ///连续的下一个                        {                            if(s[k]=='G')break;///后面必须都是G了,这种已经算过了                            if(s[k]=='R')R++;                            if(R==R_all)ans=(ans+1)%mod;///前i个是G,k位后面全是G,RG中间交替                        }                    }                }                if(flag)break;            }            long long ret=1;            for(int i=0; i<num; ++i)                ret=(ret*2)%mod;            ans=(ret-ans+mod)%mod;            printf("%I64d\n",ans);        }    }}




原创粉丝点击