【CodeForces - 1149B】Three Religions(dp查询)

博客围绕栈操作问题展开,题目包含q个操作,有加字符和删字符两种,每次操作后需判断是否满足特定情况。作者首次遇到用dp来更新查询答案的此类问题,认为很有意思且可延伸诸多问题,还给出了代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接
题目大意是给三个栈相当于,q个操作,一种是指定加在某个队列一个字符,第二种是删掉某个栈的一个字符。然后每次操作完问是否满足下列情况:
The three religions can coexist in peace if their descriptions form disjoint subsequences of the Word of Universe. More formally, one can paint some of the characters of the Word of Universe in three colors:1 ,2 ,3 , so that each character is painted in at most one color, and the description of the -th religion can be constructed from the Word of Universe by removing all characters that aren’t painted in color .

貌似第一次见这种类型的用dp来更新查询答案的,感觉挺有意思的,感觉又可以延伸好多问题。
代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<set>
#define INF 0x7f7f7f7f
#define maxx 100005
#define mod 1000000007
using namespace std;
typedef long long ll;
int n,q;
char s[maxx];

int _next[maxx][26];
int pos[26];

int f[255][255][255];

int r1[255],len1;
int r2[255],len2;
int r3[255],len3;
int main()
{
    cin>>n>>q;
    scanf("%s",s+1);
    memset(pos,INF,sizeof(pos));
    for(int i=n;i>=0;i--)
    {
        for(int j=0;j<26;j++)
            _next[i][j]=pos[j];
        if(i)pos[s[i]-'a']=i;
    }
    memset(f,INF,sizeof(f));
    f[0][0][0]=0;
    char c[5];
    int x;
    while(q--)
    {
        scanf("%s",c);
        if(c[0]=='+')
        {
            scanf("%d%s",&x,c);
            int now=c[0]-'a';
            if(x==1)
            {
                r1[++len1]=now;
                for(int j=0;j<=len2;j++)
                    for(int k=0;k<=len3;k++)
                    {
                        f[len1][j][k]=INF;
                        if(f[len1-1][j][k]<=n)
                            f[len1][j][k]=_next[f[len1-1][j][k]][now];                        
                        if(j&&f[len1][j-1][k]<=n)
                            f[len1][j][k]=min(f[len1][j][k],_next[f[len1][j-1][k]][r2[j]]);

                        if(k&&f[len1][j][k-1]<=n)
                            f[len1][j][k]=min(f[len1][j][k],_next[f[len1][j][k-1]][r3[k]]);
                    }
            }
            else if(x==2)
            {
                r2[++len2]=now;
                for(int i=0;i<=len1;i++)
                    for(int k=0;k<=len3;k++)
                {
                    f[i][len2][k]=INF;
                    if(f[i][len2-1][k]<=n)
                        f[i][len2][k]=_next[f[i][len2-1][k]][now];
                    if(i&&f[i-1][len2][k]<=n)
                        f[i][len2][k]=min(f[i][len2][k],_next[f[i-1][len2][k]][r1[i]]);
                    if(k&&f[i][len2][k-1]<=n)
                        f[i][len2][k]=min(f[i][len2][k],_next[f[i][len2][k-1]][r3[k]]);
                }
            }
            else
            {
                r3[++len3]=now;
                for(int i=0;i<=len1;i++)
                    for(int j=0;j<=len2;j++)
                {
                    f[i][j][len3]=INF;
                    if(f[i][j][len3-1]<=n)
                        f[i][j][len3]=_next[f[i][j][len3-1]][now];
                    if(i&&f[i-1][j][len3]<=n)
                        f[i][j][len3]=min(f[i][j][len3],_next[f[i-1][j][len3]][r1[i]]);
                    if(j&&f[i][j-1][len3]<=n)
                        f[i][j][len3]=min(f[i][j][len3],_next[f[i][j-1][len3]][r2[j]]);
                }
            }
        }
        else
        {
            scanf("%d",&x);
            if(x==1)len1--;
            else if(x==2)len2--;
            else len3--;
        }
        if(f[len1][len2][len3]<=n)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值