表情匹配总数 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);
}
给定一个长度为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);
}
相关文章推荐
- Android实战技巧:如何在ScrollView中嵌套ListView
- [GitHub]简单几步将你的项目托管到GitHub
- 基于RHCS+iSCSI+CLVM实现Web服务的共享存储集群架构
- Android测试 ---- Espresso + Jacoco
- 第七周 数据结构实践项目-对栈【项目5 -排队看病模拟】
- 20151103 IBM AIX oracle shutdown,shared pool分析
- 这些幽默只有程序员才懂?
- 初识JNI(一)
- C++中四种强制类型转换的区别
- ssh初学
- 检测IE浏览器方法
- 有趣的UML
- hdu5525
- iOS开发 实现QQ第三方登录
- leetcode-Excel Sheet Column Number
- Qt 5 常见错误汇总
- 实现数组元素的逆序
- jQuery第二天, jQuery操作DOM小结(初级)
- Linux 源列表
- 利用insert,update和delete注入获取数据