表情匹配总数 DP SRM 671 div1 300 BearCries

本文介绍了一个特定字符串问题的动态规划解决方案。该问题要求计算一个由';'和'_组成的字符串能以多少种方式分解为合法的Cry表情(即两个';'间至少有一个'_')。通过定义状态f[i][a][b]来跟踪到第i个字符时的合法表情组合数量,文章详细阐述了转移方程,并提供了完整的代码实现。

题意:

给定一个长度为N(N<=200)的字符串 字符串中只含有”;”和”_“两种字符 一个合法的Cry表情为两个”;”中间含有至少一个”_“

一个合法Cry表情为字符串的一个子串(不必在原串中连续)

求题目中给出的字符串分解成若干个合法Cry表情的方案数(原字符串中的每个字符都必须出现在某个Cry表情中并且只能出现一次)

题解:

问题可以由dp解决

问题可以由dp解决

我们考虑状态f[i][a][b]表示当前考虑到第i个字符

use a to represent the current number of open emoticonswithout "_" and b  thenumber of open emoticons with at least one "_"

• If s[i] is ; we can use it to open or close anemoticon.

•         If s[i] opens an emoticon then thenumber of currently open emoticons increase by 1: a=a+1

           If s[i] closes anemoticon then we can only pick one of the bemoticons that can be closedand      close it. b will decrease.

• If s[i] is "_" then it must be assigned to anopen emoticon.

           We can assign it to one ofthe a emoticons that lack a "_". There area options intotal and this decreases a while decreasing b.

             We canassign it to one of the b emoticons that already contain one or more"_". There are b optionsand a and b remain unchanged.

• Move to the next character, i increases.

const int MODD=1000000007;
string mmessage;
long long dp[201][67][67];
int n;
long long haha(int i,int a,int b)
{
    long long res=dp[i][a][b];
    if(res==-1)
    {
        res=0;
        if(i==n)
        {
            if(a==0&&b==0)res=1;
        }
        else
        {
            if(mmessage[i]==';')
            {
                if(a<n/3)(res+=(haha(i+1,a+1,b))%MODD)%MODD;
                if(b>0) (res+=(b*haha(i+1,a,b-1))%MODD)%MODD;
            }
            else
            {
                if(a>0)(res+=(a*haha(i+1,a-1,b+1))%MODD)%MODD;
                if(b>0) (res+=(b*haha(i+1,a,b))%MODD)%MODD;
            }
        }
        res%=MODD;
        dp[i][a][b]=res;
        cout<<res<<endl;
    }
    return res;
}
class BearCries
{
public:
    int count(string message)
    {
        mmessage = message;
        //cout<<mmessage<<endl;
        n=mmessage.size();
        memset(dp,-1,sizeof(dp));
        return (int)haha(0,0,0);
    }



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值