您的位置:首页 > 其它

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

2015-11-03 19:46 399 查看
题意:
给定一个长度为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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: