Colorful Cupcakes

Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

Beaver Bindu has N cupcakes. Each cupcake has one of three possible colors. In this problem we will represent the colors by uppercase letters \'A\', \'B\', and \'C\'. Two cupcakes of the same color are indistinguishable. You are given a String cupcakes consisting of exactly N characters. Each character in cupcakes gives the color of one of Bindu\'s cupcakes.
Bindu has N friends, sitting around a round table. She wants to give each friend one of the cupcakes. Moreover, she does not want to give cupcakes of the same color to any pair of friends who sit next to each other.
Let X be the number of ways in which she can hand out the cupcakes to her friends. As X can be very large, compute and return the value (X modulo 1, 000, 000, 007).

输入

The first line contains one integer T(1 ≤ T ≤ 60)—the number of test cases. Each of the next T lines contains one string. Each string will contain between 3 and 50 characters, inclusive. Each character in the string will be either \'A\', \'B\', or \'C\'.

输出

For each test case. Print a single number X — the number of ways in which she can hand out the cupcakes to her friends.

示例输入

3
ABAB
ABABA
ABABABABABABABABABABABABABABABABABABABABABABABABAB

示例输出

2
0
2

提示

来源

2014年山东省第五届ACM大学生程序设计竞赛

示例程序

这几天一直在看区间dp,下意识的相用区间dp做发现不可行,搜了半天才看到一个题解说是用4维dp做

以前没接触过也就想不到用dp【i,a,b,j】来表示第i个小朋友,之前已经吃了a个A蛋糕和b个B蛋糕现在吃第j种蛋糕的方案数(j=1,2,3)(a+b+c=i)

然后枚举第一次吃A OR B OR C蛋糕的情况 然后就是细节问题了

ACcode:

#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define maxn 55
#define mod 1000000007
#define inf 0x3f3f3f
using namespace std;
/**
所有数据不超过50蛋糕只有3种,4维dp可以搞,枚举A,B吃了多少个,
再枚举这个人吃什么,按照人数从1推到n就可以了,由于人是成环形,
还要注意第n个和第1个人不吃相同的。所以根据第1个人吃什么,
做3遍上述的dp,统计答案就行。
**/
char s[maxn];
int dp[maxn][maxn][maxn][4];///dp[i,a,b,j]到第i个人时候吃了a个蛋糕A蛋糕b个B蛋糕次到j类蛋糕
int num[4],na,nb,nc;///记录每种蛋糕的个数
long long ans;///答案
int main(){int t;scanf("%d",&t);while(t--){ans=0;scanf("%s",s+1);int len=strlen(s+1);memset(num,0,sizeof(num));na=num[1]=count(s+1,s+1+len,'A');nb=num[2]=count(s+1,s+1+len,'B');nc=num[3]=count(s+1,s+1+len,'C');for(int loop=1;loop<=3;++loop){if(num[loop]){///下面是第一个吃A,B,C蛋糕的初始化memset(dp,0,sizeof(dp));if(loop==1)dp[1][1][0][1]=1;else if(loop==2)dp[1][0][1][2]=1;else dp[1][0][0][3]=1;for(int i=2;i<=len;++i)for(int a=0;a<=min(i,na);++a)for(int b=0;b<=min(i-a,nb);++b){int c=i-a-b;if(a&&((i!=2&&i!=len)||loop!=1))///第i个小朋友吃A蛋糕的情况dp[i][a][b][1]=(dp[i][a][b][1]+dp[i-1][a-1][b][2]+dp[i-1][a-1][b][3])%mod;if(b&&((i!=2&&i!=len)||loop!=2))///第i个小朋友吃B蛋糕的情况dp[i][a][b][2]=(dp[i][a][b][2]+dp[i-1][a][b-1][1]+dp[i-1][a][b-1][3])%mod;if(c&&((i!=2&&i!=len)||loop!=3))///第i个小朋友吃C蛋糕的情况dp[i][a][b][3]=(dp[i][a][b][3]+dp[i-1][a][b][1]+dp[i-1][a][b][2])%mod;}long long tmp;tmp=(dp[len][na][nb][1]+dp[len][na][nb][2]+dp[len][na][nb][3])%mod;ans=(ans+tmp)%mod;}}printf("%lld\n",ans);}return 0;
}

山东省第五届ACM大学生程序设计竞赛 Colorful Cupcakes相关推荐

  1. 山东省第五届ACM大学生程序设计竞赛 Weighted Median

    Weighted Median Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 For n elements x1, x2, .. ...

  2. SDUT 2879 Colorful Cupcakes (2014年山东省第五届ACM大学生程序设计竞赛)

    Colorful Cupcakes Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Beaver Bindu has N cupc ...

  3. 河南省第五届acm大学生程序设计竞赛总结

    河南省第五届acm大学生程序设计竞赛总结 河南省第五届acm大学生程序设计竞赛最终排名 首先热烈祝贺我校ACM队在本次比赛中获得四金三银的成绩,而我们队也获得金牌一枚!!! 五月十三号河南省第五届ac ...

  4. 河南工程学院第五届ACM大学生程序设计竞赛(部分题解)

    河南工程学院第五届ACM大学生程序设计竞赛(部分题解) 问题 A: 敏感的小明同学 小明是一个对数字非常敏感的人,当他看到某个特定的数字p (1<=p<=9)时就会兴奋一下,现在给你一个数 ...

  5. 2013年山东省第四届ACM大学生程序设计竞赛 Problem I Thrall’s Dream 图,2n遍dfs

    Thrall's Dream Time Limit: 1000MS Memory limit: 65536K 题目描述 We never paid any heed to the ancient pr ...

  6. 2013年山东省第四届ACM大学生程序设计竞赛

    Rescue The Princess Time Limit: 1000MS    Memory limit: 65536K 题目描述 Several days ago, a beast caught ...

  7. 山东省第五届ACM省赛题——Colorful Cupcakes(四维dp)

    题目描述 Beaver Bindu has N cupcakes. Each cupcake has one of three possible colors. In this problem we ...

  8. “不念过往,不畏将来”——2017年山东省第八届ACM大学生程序设计竞赛总结

    今天去参加了第八届山东ACM省赛,也是自己第一次参加正式的ACM比赛,有诸多感想. 先说说去比赛的经过吧,整个大体上还是比较顺利的,青科大的志愿者也十分的负责用心(排队排的很有意思),住宿环境也还不错 ...

  9. 山东省第四届ACM大学生程序设计竞赛 Thrall’s Dream(单源强连通分量)

    Thrall's Dream Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We never paid any heed to ...

最新文章

  1. MVC URL参数传递+变为空格解决方法
  2. 对c语言课程的心得体会,C语言课程设计心得体会
  3. 社交电商风潮起,中小卖家何处去
  4. 四十五、Stata检验方法,回归分析与生存分析
  5. android 格式格式:YYYYMMDDHH24MISS 的时间戳timestamp
  6. checkStyle -- 代码风格一致
  7. 如何选择python书籍_关于 Python 的经典入门书籍有哪些?
  8. CSAPP lab3 bufbomb-缓冲区溢出攻击实验(下)bang boom kaboom
  9. python画图小实例_python绘图实例
  10. 一键清除系统垃圾 bat文件
  11. Java类加载机制由浅入深
  12. python3输入列表_[Python3] 列表的基本用法[TZZ]
  13. Macbook 修改照片的大小
  14. java实现给图片添加水印
  15. python 求向量间内积 和外积
  16. 电脑插上耳机后声音依然外放,简单解决两步走!
  17. 全球及中国精密加工零件行业市场需求及未来发展展望报告2022-2028年
  18. Word基础(三十四)引文与书目
  19. GPFS各类排故日志收集汇总
  20. 英汉互译教程---生词

热门文章

  1. SQL Server的优点与缺点
  2. IE浏览器的这个代理服务总是被自动勾选怎么办,取消之后还是会被自动勾选!!!!!!!!!!!!(暂时找到“凶手了”)
  3. 人机博弈小游戏(Java)
  4. 为什么要阅读——兼分享《首先,打破一切常规》[中译文]:世界顶级管理者的成功秘诀/(美)马库斯·白金汉,(美)柯特·科夫曼 著
  5. 高级语言?编译程序?解释程序?目标代码?
  6. 西京学院学位计算机题库和答案,西京学院 学位英语 普通英语 精彩试题整理.doc...
  7. 使用Qt构建ROS应用程序
  8. ACM 各大OJ平台以及题目分类
  9. SVN出现黄色感叹号,红绿双箭头
  10. layui导出excel动态拆分单元格一个单元格显示多行数据合并单元格