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 ...
随机推荐
- SpringMVC框架
一.SpringMVC工作流程图 DispatcherServlet:Spring提供的前端控制器,所有的请求都有经过它来统一分发.在DispatcherServlet将请求分发给Spring Con ...
- C#导入导出Excel表的数据
一:C#导入导出EXCEL文件的类 代码如下: 首先将Microsoft Excel 14.0 Object Library 引用导入 using System; using System.Data; ...
- 快速了解Scala技术栈
http://www.infoq.com/cn/articles/scala-technology/ 我无可救药地成为了Scala的超级粉丝.在我使用Scala开发项目以及编写框架后,它就仿佛凝聚成为 ...
- ASP读取RSS
<% @language="VBScript"%> <% Function readrss(xmlseed) dim xmlDoc dim http Set ht ...
- 对象创建型模式------Singleton(单例模式)
地址:http://blog.csdn.net/wuzhekai1985/article/details/6665869.仅供自己参考学习. 单例模式:保证一个类仅有一个实例,并提供一个访问它的全局节 ...
- UDP 多播 Java
1.服务端 public class UdpMulticastServer { /** * @param args */ public static void main(String[] args) ...
- UDP通信 Java
public class UdpServerTest { /** * @param args * @throws SocketException */ public static void main( ...
- 【JAVA】在编译期可直接替换的final变量
一.满足以下三个条件,一个final变量就不再是一个变量,而是一个直接量. 使用final修饰符修饰. 在申明的时候就进行初始化 初始化的值在编译器就可以确定. 二.在什么情况下初始化的值在编译期是可 ...
- (十一)Hibernate 高级配置
第一节:配置数据库连接池 反问数据库,需要不断的创建和释放连接,假如访问量大的话,效率比较低级,服务器消耗大: 使用数据库连接池,我们可以根据实际项目的情况,定义连接池的连接个数,从而可以实现从连接池 ...
- 客官,您的 Flask 全家桶请收好
http://www.factj.com/archives/543.html Flask-AppBuilder - Simple and rapid Application buil ...