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

来源:互联网 发布:淘宝 seo 教程 编辑:程序博客网 时间:2024/06/05 06:25

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.
 

示例输入

3ABABABABAABABABABABABABABABABABABABABABABABABABABABABABABAB

示例输出

202


分析:当时我们团队赛的时候根本没有什么思路,看的别人的题解,用四维的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 1000000007using 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;elsedp[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;}



1 0