csuoj 1354: Distinct Subsequences
这个题是计算不同子序列的和;
spoj上的那个同名的题是计算不同子序列的个数;
其实都差不多;
计算不同子序列的个数使用dp的思想;
从头往后扫一遍
如果当前的元素在以前没有出现过,那么dp[i]=dp[i-1]*2+1;
不然找到最右边的这个元素出现的位置j;
dp[i]=d[i]*2-dp[j];
spoj代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mod 1000000007
using namespace std; char s[];
int pos[];
int biao[];
int dp[]; int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
memset(biao,,sizeof biao);
scanf("%s",s+);
n=strlen(s+);
for(int i=; i<=n; i++)
{
pos[i]=biao[s[i]-'A'];
biao[s[i]-'A']=i;
}
dp[]=;
for(int i=; i<=n; i++)
{
if(pos[i]==)
{
dp[i]=(dp[i-]*+);
while(dp[i]>=mod)dp[i]-=mod;
}
else
{
dp[i]=(dp[i-]*-dp[pos[i]-]);
if(dp[i]<)dp[i]+=mod;
while(dp[i]>=mod)dp[i]-=mod;
}
}
printf("%d\n",dp[n]+);
}
return ;
}
csuoj代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mod 1000000007
using namespace std; char s[];
int pos[];
int biao[];
int dp[];
long long sum[]; int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
memset(biao,,sizeof biao);
scanf("%s",s+);
n=strlen(s+);
for(int i=; i<=n; i++)
{
pos[i]=biao[s[i]-''];
biao[s[i]-'']=i;
}
dp[]=;
for(int i=; i<=n; i++)
{
long long tmp=sum[i-];
if(pos[i]==)
{
dp[i]=dp[i-]*;
if((s[i]-'')>)dp[i]+=;
while(dp[i]>=mod)dp[i]-=mod;
}
else
{
dp[i]=(dp[i-]*-dp[pos[i]-]);
tmp-=sum[pos[i]-];
if(tmp<)tmp+=mod;
if(dp[i]<)dp[i]+=mod;
while(dp[i]>=mod)dp[i]-=mod;
}
sum[i]=(tmp*+(long long)(dp[i]-dp[i-])*(s[i]-'')+sum[i-])%mod;
if(sum[i]<=)sum[i]+=mod;
}
printf("%lld\n",sum[n]);
}
return ;
}
csuoj 1354: Distinct Subsequences的更多相关文章
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...
- Leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode(115) Distinct Subsequences
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences Leetcode
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【leetcode】Distinct Subsequences(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
随机推荐
- 关于Eclipse插件开发(一)
plugin.xml是插件和Eclipse内核的接口,Eclipse就像一所大宅子.它的外墙(plugin.xml)有很多门(扩展点), 要熟练进入这座大宅子,就得先搞清楚它有那些门(扩展点). 插件 ...
- HuffmanTree && HuffmanCode
如何构造HuffmanTree? Huffman算法: (1)根据给定的n个权值{w1, w2, ...,wn}构成n棵二叉树集合
- CSS常用布局实现方法
CSS 布局对我来说,既熟悉又陌生.我既能实现它,又没有很好的了解它.所以想总结一下,梳理一下 CSS 中常用的一列,两列,三列布局等的实现方法.本文小白,仅供参考.但也要了解下浮动,定位等. 一.一 ...
- JAXB - The Object Factory
Usually hidden in the middle of the list of the classes derived from the types defined in an XML sch ...
- 关于数据导出到Excel科学计数法的处理
SELECT '=T("'+字段+'")' from table 在这里在显示的字段内容前加了 '=T("',在后面也加了'")'.在这这里T()是Exc ...
- web开发常用图片格式
web开发常用图片格式有:gif jpg/jpeg png gif:图片压缩率高,可以显示动画,但是只能显示256色,可能造成颜色丢失. jpg:图片压缩率高(有损压缩),可以用小文件来显示 ...
- Android Metro风格的Launcher开发系列第一篇
前言:从毕业到现在已经三年多了,回忆一下这三年基本上没有写过博客,总是觉得忙,没时间写,也觉得写博客没什么大用.但是看到很多大牛们都在写博客,分享自己的东西,所以嘛本着向大牛看齐,分享第一,记录第二的 ...
- Razor语法大全(转)
Razor语法大全 因为最近在看mvc的时候在学习Razor的发现了这个不错的博文,故转之. 本文页面来源地址:http://www.cnblogs.com/dengxinglin/p/3352078 ...
- 微软阵营企稳的利好消息:.NET开源、Visual Studio免费
今天各个IT社区,头版头条说的是微软.NET开源了.宇宙中最好的IED–Visual Studio Community 2013将免费提供给用户的消息. <宇宙中最强大的开发环境免费了!> ...
- iOS 安全:UIWebView访问Https站点防止中间人攻击
尽管Https协议能够提供数据的加密.身份的认证等安全服务,但并不是没有漏洞.HTTPS协议安全隐患的存在可能使用户受到各种极具破坏力的网络攻击.其中中间人攻击(Man In The Middle, ...