【250】
Beaver Bindu has some colored bricks. Each color is described by an uppercase letter. Bricks of each color all look exactly the same. You are given a string bricks. Each character of bricks represents the color of one of Bindu's bricks.
Bindu wants to arrange all her bricks into a row. A row of bricks is nice if there is at most one pair of adjacent bricks which have different colors. Return the number of ways in which Bindu can form a nice row, using all her bricks. (Two ways are considered identical if they correspond to the same sequence of brick colors.)显然,如果只有一种颜色,就只有一中排列方案;如果有两种颜色(A、B),就只有两种方案(AB、BA);如果有大于两种颜色,就没有符合题意的方案
class ColorfulBricks{ public: int countLayouts(string s){ int cnt[100],num=0; memset(cnt,0,sizeof(cnt)); for(int i=0;i2) return 0; else return num; }};
【500】
Beaver Bindu has some chocolates arranged in a row. The wrapping of each chocolate has a single color. Multiple chocolates can share the same color. In this problem, each of the possible colors is represented by an uppercase letter. You are given a string chocolates. For each i, the i-th chocolate (0-based index) in the row has the color chocolates[i].
The spread of a row of chocolates is the maximum number of adjacent chocolates that all share the same color. Formally, the spread can be defined as the maximum value of (j-i+1), where i <= j and all the chocolates in the positions between i and j, inclusive, have the same color.
You are also given an int maxSwaps. Bindu can swap any two adjacent chocolates. She has decided to make at most maxSwaps such swaps.
Return the maximum spread she can obtain.
因为所给的n很小(n≤50),所以幂次的方法也可以接受。
枚举一个字母为“参照物”,它是不动的,而其他的围绕着它来交换并向它靠拢。处理完所有相同字母的距离之后,将距离排序,从小到大相加,直到最大且小于等于maxstep,更新答案。
templateinline void gmax(T &a,T b){if(a inline void gmin(T &a,T b){if(a>b)a=b;}class ColorfulChocolates{ public: int maximumSpread(string s,int k){ int dist[200],len=s.size(),ans=0; for(int i=0;i =0;j--) if(s[i]==s[j]) cnt++,dist[j]=i-j-cnt; sort(dist,dist+len); int tmp=0,x=0,t_ans=1; while(tmp+dist[x]<=k) tmp+=dist[x],t_ans++,x++; gmax(ans,t_ans); } return ans; }};
【950】
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).
五维DP:f[x][i][j][k][l]表示前x个数,用了i个A,j个B,k个C,第x-1位是l(l=1,2,3) 的方案数,至于首尾不相同的问题,可以枚举第一个的颜色,然后后面的DP。
class ColorfulCupcakesDivTwo{ public: int countArrangements(string s){ const int BASE=1000000007; int f[51][51][51][51][4],A(0),B(0),C(0),n=s.size(),ans(0); for(int i=0;i
但是,上面这种代码是错误的!!!!自己算算多少空间把……绝对会开爆了内存。怎么办怎么办?
我们发现在转移时总是i+j+k==x,所以k=x-i-j,当i和j一定时,枚举k是多余的,所以可以压掉一维,正确代码如下:
class ColorfulCupcakesDivTwo{ public: int countArrangements(string s){ int A(0),B(0),C(0),n=s.size(),ans=0; const int BASE=1000000007; int f[51][51][51][4]; for(int i=0;i
人生第一次TC,最后一题因为最后时刻没有点Compile而直接submit失去了AK的机会,分数只有670分(Cha人家的很爽)。名字颜色变成黄色了,下次只能做Div1了,不知道Div1的题目有多难。反正Div2感觉是挺水的。