Tc srm 551 div2

Source: Internet
Author: User

Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove

I finally passed two questions in TC. Unfortunately, the third question was wrong.

250pt: only several colors are required.

class ColorfulBricks{public:int countLayouts(string bricks){bool flag[26];int cnt=0;memset(flag,false,sizeof(flag));for(int i=0;i<bricks.size();i++)if(!flag[bricks[i]-'A']){flag[bricks[i]-'A']=true;cnt++;}if(cnt==1)return 1;if(cnt==2)return 2;return 0;}};

500pt

class ColorfulChocolates{public:int maximumSpread(string chocolates, int maxSwaps){int mmax=1;int len=chocolates.size();for(int i=0;i<26;i++){for(int j=0;j<len-1;j++){int a=j,b=j+1;int tmp=0;int step=maxSwaps;bool flag[60];memset(flag,true,sizeof(flag));for(int x=a,y=b;x>=0||y<len;x--,y++){if(x>=0&&x<len&&chocolates[x]==i+'A'){if(step>=a-x){tmp++;step-=a-x;a--;}}if(y>=0&&y<len&&chocolates[y]==i+'A'){if(step>=y-b){tmp++;step-=(y-b);b++;}    }}mmax=max(mmax,tmp);}}return mmax;}};

1000pt: 26 colors are used in the game, and the polyA count using multiple sets and matrices is useless. There are only three colors, and the DP can be broken. I wrote two versions, and the first one was tested on the local machine. The traffic was not mentioned, but the memory should be too large.

Note: DP [POS] [a] [B] [C] [kind] indicates the position of the previous pos. a A, B, and C are used. The last one is the kind number. Enumeration start point.

Type 2: DP [FST] [pre] [a] [B] [C] indicates that the first one is FST, and the last one is pre. a A is used, number of B and C types. Memory-based search can be broken.

int dp[3][3][51][51][51];int cnt[3],n;class ColorfulCupcakesDivTwo{public:int slove(int pos,int pre,int fst,int a,int b,int c){if(a<0||b<0||c<0)return 0;if(pos==n)return pre!=fst;if(dp[fst][pre][a][b][c]!=-1)return dp[fst][pre][a][b][c];int ret=0;if(pos==0){ret=(ret+slove(pos+1,0,0,a-1,b,c))%MOD;ret=(ret+slove(pos+1,1,1,a,b-1,c))%MOD;ret=(ret+slove(pos+1,2,2,a,b,c-1))%MOD;}else if(pre==0){ret=(ret+slove(pos+1,fst,1,a,b-1,c))%MOD;ret=(ret+slove(pos+1,fst,2,a,b,c-1))%MOD;}else if(pre==1){ret=(ret+slove(pos+1,fst,0,a-1,b,c))%MOD;ret=(ret+slove(pos+1,fst,2,a,b,c-1))%MOD;}else if(pre==2){ret=(ret+slove(pos+1,fst,0,a-1,b,c))%MOD;ret=(ret+slove(pos+1,fst,1,a,b-1,c))%MOD;}return dp[fst][pre][a][b][c]=ret;}int countArrangements(string cupcakes){cnt[0]=cnt[1]=cnt[2]=0;n=cupcakes.size();for(int i=0;i<n;i++)cnt[cupcakes[i]-'A']++;memset(dp,-1,sizeof(dp));return slove(0,0,0,cnt[0],cnt[1],cnt[2]);}};/*int dp[51][51][51][51][3];int countArrangements(string cupcakes){cnt[0]=cnt[1]=cnt[2]=0;int n=cupcakes.size();for(int i=0;i<n;i++)cnt[cupcakes[i]-'A']++;int ans=0;for(int s=0;s<3;s++){memset(dp,0,sizeof(dp));if(s==0)dp[1][1][0][0][0]=1;else if(s==1)dp[1][0][1][0][1]=1;elsedp[1][0][0][1][2]=1;for(int x=2;x<=n;x++){for(int a=0;a<=cnt[0];a++)for(int b=0;b<=cnt[1];b++)for(int c=0;c<=cnt[2];c++)if(a+b+c==x){if(a){dp[x][a][b][c][0]=(dp[x][a][b][c][0]+dp[x-1][a-1][b][c][1])%MOD;dp[x][a][b][c][0]=(dp[x][a][b][c][0]+dp[x-1][a-1][b][c][2])%MOD;}if(b){dp[x][a][b][c][1]=(dp[x][a][b][c][1]+dp[x-1][a][b-1][c][0])%MOD;dp[x][a][b][c][1]=(dp[x][a][b][c][1]+dp[x-1][a][b-1][c][2])%MOD;}if(c){dp[x][a][b][c][2]=(dp[x][a][b][c][2]+dp[x-1][a][b][c-1][1])%MOD;dp[x][a][b][c][2]=(dp[x][a][b][c][2]+dp[x-1][a][b][c-1][0])%MOD;}}}ans+=(((-dp[n][cnt[0]][cnt[1]][cnt[2]][s]+dp[n][cnt[0]][cnt[1]][cnt[2]][0]+dp[n][cnt[0]][cnt[1]][cnt[2]][2]))+dp[n][cnt[0]][cnt[1]][cnt[2]][1])%MOD;    cout<<ans<<endl;}return ans;}*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.