A Color Game

题目链接:https://codeforces.ml/gym/102835/submit

题意:给定7种字符 每有一段连续的长度大于m的相同字符就能消去 问最终整段字符串能否消去

dp[i][j][x] 代表 i到j区间 剩种类为x的数最大为多少

那么每次区间合并的时候 还是 dp[i][j][x]=max(dp[i][k][x]+dp[k+1][j][x],dp[i][j][x]) 如果当前的区间段有能够删除的段

那么 其他的字符串的当前区间段的数的非法状态就可以置为0 保证能把断开的字符串接起来  

注意 长度为1的要特判     开始全部置为-1 为非法状态   最后只有某一个字符串能在[1,n]整段消去的时候才能保证yes

 
 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 #define pb push_back
 4 using namespace std;
 5 const int maxn=5e2+10;
 6 const int mod=1e9+7;
 7 int dp[maxn][maxn][10];
 8 char s[maxn];
 9 map<char,int>mp;
10  
11  
12 int main()
13 {
14     ios::sync_with_stdio(0);
15     cin.tie(0);
16     cin>>(s+1);
17     int n=strlen(s+1);
18     mp['R']=1;mp['G']=2;mp['B']=3;
19     mp['C']=4;mp['M']=5;mp['Y']=6;
20     mp['K']=7;
21     int m;
22     cin>>m;
23     if(m==1)
24     {
25         cout<<"Yes"<<'\n';
26         return 0;
27     }
28     for(int i=0;i<maxn;i++)
29         for(int j=0;j<maxn;j++)
30             for(int x=0;x<8;x++)
31                 dp[i][j][x]=-1e9;
32     for(int i=1;i<=n;i++)
33     {
34         dp[i][i][mp[s[i]]]=1;
35     }
36     for(int len=2;len<=n;len++)
37     {
38         for(int i=1;i+len-1<=n;i++)
39         {
40             int j=i+len-1;
41             int f=0;
42             for(int k=i;k<j;k++)
43             {
44                 for(int x=1;x<=7;x++)
45                 {
46                     dp[i][j][x]=max(dp[i][j][x],dp[i][k][x]+dp[k+1][j][x]);
47                     if(dp[i][j][x]>=m)
48                         f=1;
49                 }
50             }
51             for(int x=1;x<=7;x++)
52             {
53                 if(dp[i][j][x]<0)
54                 {
55                     if(f)
56                         dp[i][j][x]=0;
57                 }
58             }
59         }
60     }
61  
62     for(int i=1;i<=7;i++)
63     {
64         if(dp[1][n][i]>=m)
65         {
66             cout<<"Yes"<<'\n';
67             return 0;
68         }
69     }
70     cout<<"No"<<'\n';
71  
72 }
View Code

 

posted @ 2020-12-21 22:21  canwinfor  阅读(227)  评论(0)    收藏  举报