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,相邻2个R的距离相等且距离为奇
证明不难,反证法,假设其中有一对相邻距离为偶数,则必能找到中间的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); } }}
- hdu 4346 The Beautiful Road(思维,枚举,5级)
- hdu - 4346 - beautiful road - 枚举+想法
- HDU 4346 The Beautiful Road 思路题
- HDU 1598 find the most comfortable road (枚举+Kruskal)
- HDU 1598 find the most comfortable road 枚举 + kruskal算法
- HDU 1598 find the most comfortable road (枚举+Kruskal)
- HDU 1598 find the most comfortable road (Kruskal + 枚举)
- 2012 Multi-University Training Contest 5 Problem G The Beautiful Road(HDU4346)
- hdu 1598 find the most comfortable road(并查集+枚举图的各边)
- 【并查集+枚举】杭电 hdu 1598 find the most comfortable road
- hdu 1598 find the most comfortable road(枚举+并查集)
- hdu 1598 find the most comfortable road(枚举+并查集)
- HDU&POJ 1598 find the most comfortable road(枚举+最小生成树)
- HDU 1598 Find the most comfortable road 二分枚举答案 或 并查集
- HDU-1598-find the most comfortable road(暴力枚举+Kruskal最小生成树)
- hdu 1598 find the most comfortable road (并查集 + 枚举)
- Hdu 1598 find the most comfortable road 并查集&枚举
- HDU 1598 find the most comfortable road 【枚举+(并查集)最小生成树Kruskal】
- Ajax介绍
- 几个 关于时间 的函数
- ajax学习总结2
- WPF 4的九大新特性全解析
- java io 流
- hdu 4346 The Beautiful Road(思维,枚举,5级)
- python类-各种method
- jQuery学习13---网站列表增加了判断有没有一个对象
- PHP 基于文本系统的简易的聊天室
- JS获取GET参数
- 选择排序
- C# protected internal和internal的区别
- Windows gVim 多标签设置
- hive集群安装配置