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

原创 2016年06月02日 10:31:55

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


分析:当时我们团队赛的时候根本没有什么思路,看的别人的题解,用四维的dp数组,dp[ i ][ a ][ b ][ j ],

i 代表长度为i,a代表选A的个数,b代表选B的个数,因为就三种,A,B确定了C自然就确定了,所以不需要记录了,

j代表该次选择的是A,还是B,还是C。

dp[ i ][ a ][ b ][ 1 ] = dp[ i-1 ][ a ][ b ][ 2 ] + dp[ i-1 ][ a ][ b ][ 3 ]; //该位置选A,前一个只能是B或C,其它类似

dp[ i ][ a ][ b ][ 2 ] = dp[ i-1 ][ a ][ b ][ 1 ] + dp[ i-1 ][ a ][ b ][ 3 ];

dp[ i ][ a ][ b ][ 3 ] = dp[ i-1 ][ a ][ b ][ 1 ] + dp[ i-1 ][ a ][ b ][ 2 ];

ACcode:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define MAX 55
#define MOD 1000000007
using namespace std;
int dp[MAX][MAX][MAX][MAX];
int main()
{
	int T;
	int len,i,j,k;
	char str[55];
	int na,nb,nc,loop,a,b,num[4];
	scanf("%d",&T);
	while(T--)
	{
		scanf("%s",str);
		len=strlen(str);
		na=num[1]=count(str,str+len,'A');
		nb=num[2]=count(str,str+len,'B');
		nc=num[3]=count(str,str+len,'C');
		int ans=0;
		for(loop=1;loop<=3;loop++)
		{
			memset(dp,0,sizeof(dp));
			if(!num[loop])
				continue;
			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(i=2;i<=len;i++)
			{
				for(a=0;a<=min(i,na);a++)
				{
					for(b=0;b<=min(i-a,nb);b++)
					{
						int c=i-a-b;
						if(a && !((i==2 || i==len) && loop==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))
							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))
							dp[i][a][b][3]=(dp[i-1][a][b][1]+dp[i-1][a][b][2])%MOD;
					}
				}
			}
			int temp=(dp[len][na][nb][1]+dp[len][na][nb][2]+dp[len][na][nb][3])%MOD;
			ans=(ans+temp)%MOD;
		}
		printf("%d\n",ans);
	}
	
	return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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

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

山东省第五届ACM大学生程序设计竞赛-Weighted Median(模拟)

Weighted Median Time Limit: 2000MS Memory limit: 65536K 题目描述 For n elements x1, x2, ..., xn wit...

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

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

山东省第五届ACM大学生程序设计竞赛 angry_birds_again_and_again 求积分

首页比赛ProblemsRanklistStatusStatistics acmer(Yt_zp) 注销 angry_birds_again_and_again ...

山东省第五届ACM大学生程序设计竞赛-angry_birds_again_and_again(积分)

angry_birds_again_and_again Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 The probl...

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

Problem Description For n elements x1, x2, ..., xn with positive integer weights w1, w2, ..., wn. T...

山东省第五届ACM大学生程序设计竞赛 Hearthstone II 组合数学 Stirling数

Hearthstone II Time Limit: 2000MS Memory limit: 65536K 题目描述 The new season has begun, y...

山东省第五届ACM省赛 C - Colorful Cupcakes

4.吃蛋糕 //山东省第五届ACM省赛 C - Colorful Cupcakes /* 题意:n块蛋糕 3种颜色A,B,C 分给一个圆桌n个人 相邻两人颜色不同 思路:五维数组...

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

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

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

河南省第五届acm大学生程序设计竞赛总结      河南省第五届acm大学生程序设计竞赛最终排名      首先热烈祝贺我校ACM队在本次比赛中获得四金三银的成绩,而我们队也获得金牌一枚!!!...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)