这个题是计算不同子序列的和;

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的更多相关文章

  1. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  2. Distinct Subsequences

    https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...

  3. Leetcode Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  4. LeetCode(115) Distinct Subsequences

    题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...

  5. [Leetcode][JAVA] Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  6. Distinct Subsequences Leetcode

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  7. 【leetcode】Distinct Subsequences(hard)

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  8. 【LeetCode OJ】Distinct Subsequences

    Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...

  9. LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静

    Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of  ...

随机推荐

  1. Android 常见adb命令

    1.  查看所有已链接的设备 命令: adb devices   例: C:\Users\laiyu>adb devices List of devices attached 5d3b5aac  ...

  2. 揭开CSS3媒体查询迷雾(min-width和max-width)

    本文参考MichelleKlann的Media Queries Demystified: Min-Width and Max-Width 媒体查询(media queries)是响应式设计(Respo ...

  3. IIS启用SSL

    安全套接字层 (SSL) 是一套提供身份验证.保密性和数据完整性的加密技术.SSL 最常用来在 Web 浏览器和 Web 服务器之间建立安全通信通道.它也可以在客户端应用程序和 Web 服务之间使用. ...

  4. dagger和butterknife使用冲突

    两者会冲突的主要原因是因为两者都有:javax.annotation.processing.Processor 于是在build.gradle中添加如下配置即可: // 注释冲突 packagingO ...

  5. SQLSERVER 数据库性能的基本

    很久没有写文章了,在系统正式上线之前,DBA一般都要测试一下服务器的性能 比如你有很多的服务器,有些做web服务器,有些做缓存服务器,有些做文件服务器,有些做数据库服务器 做数据库服务器的那台服务器性 ...

  6. linux配置学习笔记(一):如何提高ssh连接的速度

    服务器端sshd配置文件 /etc/ssh/sshd_config 看是否有如下的两条配置条目 GSSAPIAuthentication no UseDNS no 如果前面带#,请把#删掉,或者新添加 ...

  7. Java_字符类(Character、String、StringBuffer)_char是基本数据类型,Character是其包装类型。

         在java中有三个类负责对字符的操作:Character.String.StringBuffer.其中,Character类是对单个字符进行操作,String是对一个字符序列的操作,Stri ...

  8. cocos2d-x实战 C++卷 学习笔记--第4章 win32平台下中文乱码问题

    前言: 将GBK编码的字符串转为UTF-8编码.(通俗点说就是解决中文乱码问题) 简要介绍: 在Win32平台下通过 log 输出中文字符时,会出现中文乱码问题.同样的代码在 ios 和 Androi ...

  9. OC 将NSString写入本地文件

    最近在公司偶尔遇到一些不经常复现的bug,为了调试,只好把关键值记录到本地文件中,在遇到问题时,调出本地文件查看一下就可以很方便的知道是不是代码逻辑的错误或者问题考虑不够周全了. 废话不多说,流程在代 ...

  10. Windows Forms(二)

    导读 1.用VS创建一个Windows Forms程序 2.分析上面的程序 3.Mediator pattern(中介者模式) 4.卡UI怎么办——BackgroundWorker组件 用VS创建一个 ...