SPOJ_DSUBSEQ Distinct Subsequences
统计一个只由大写字母构成的字符串的,子串数目,这里指的是子串不是子序列,可以不连续,请注意
然后我按照计数DP的思想,dp[i][j]表示长度为i的子串,最后一个字母为j
然后为了去重,每一次dp的时候,记录这个时候最后一位所在的位数,而且之前用一个后缀记录之后有没有该字母,这样每次,从上一次的j所处的位置的下一个看看后缀有没有这个字母,合法的话 就 dp[i][j]+=dp[i-1][k]。
但是这个方法有个超大的漏洞,就是去重的问题,我为了防止重复,虽然用了后缀记录后面有没有这个字母,但是在加的时候,我是统一加的,即不管后面有没有字母,我直接统一+dp[i-1][j],导致结果到了后面就不行了,而且这个方法会超时,我当时虽然期待他超时的,没想到返回个WA了
其实可能是因为最近计数DP做的多了的结果,这个其实用一维就可以了,从前往后扫,对当前字母,我的值即为 dp[i-1]+添加当前字母之后产生的新子串
这个新子串的个数分两种情况,
1。该字母之前未出现过,则 新个数=dp[i-1]+1,表示当前字母加上后使得前面的dp[i-1]个串又能产生新的子串,+1是指自己本身单个字母。
2.该字母之前出现过,则要判断重复了,其实就是 dp[i-1]-dp[lastoccur-1];即先加上dp[i-1]但肯定是有重复的,为了去重,找到上次出现该字母的那个地方,减去那个地方就可以去重了
要注意的是,这个里面出现了减法,而答案是要取模的,所以这种相减是可能出现负数的情况的(这个地方确实之前没想到,没注意,我还纳闷怎么其他人都有个判断负值的操作),就是因为取模里面有减法,所以是可能出现负数的,注意这种情况
#include <cstdio>
#include <iostream>
#include <cstring>
#define LL long long
using namespace std;
const int N = ;
const LL M = ;
char str[N];
LL dp[N];
int lasts[];
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%s",str+);
int len=strlen(str+);
for (int i=;i<=len;i++){
dp[i]=;
}
for (int i=;i<;i++) lasts[i]=;
for (int i=;i<=len;i++){
dp[i]=dp[i-];
if (lasts[str[i]-'A']>){
dp[i]+=dp[i-]-dp[lasts[str[i]-'A']-];
while (dp[i]<) dp[i]+=M;
}
else{
dp[i]+=dp[i-]+;
}
lasts[str[i]-'A']=i;
if (dp[i]>=M) dp[i]%=M;
}
dp[len]++;
dp[len]%=M;
printf("%lld\n",dp[len]);
}
return ;
}
SPOJ_DSUBSEQ 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 ...
随机推荐
- shell脚本中执行sql脚本(mysql为例)
1.sql脚本(t.sql) insert into test.t value ("LH",88); 2.shell脚本(a.sh 为方便说明,a.sh与t.sql在同一目 ...
- 关于2017届学长制作分享软件share(失物招领)的使用体验和需改进的内容
使用体验 1.注册界面 注册界面提示明显,提示用户输入什么类型的密码,而且输入什么样的用户名不限,注册界面色调比较单一,注册内容比较少,而且比较简单,体验感比较好,但注册界面色调和设计全无,使用感一般 ...
- CSS3实现放大缩小动画
HTML <div> <div style="height: 35px;width:300px;background:orangered;border-radius: 4p ...
- nth-of-type()的用法
同样的标签选择其中一个,就用nth-of-type() <img src="http://cms-bucket.nosdn.127.net/2018/10/16/ad8698e497e ...
- keil 生成bin文件
在 User 配置页面中,提供了三种类型的用户指令输入框,在不同组的框输入指令,可控制指令的执行时间,分别是编译前(Before Compile c/c++ file).构建前(Before Bui ...
- 报错信息 Context []startup failed due to previous errors
文章转自:http://blog.sina.com.cn/s/blog_49b4a1f10100q93e.html 框架搭建好后,启动服务器出现如下的信息: log4j:WARN No appende ...
- 070、Java面向对象之深入贯彻对象引用传递
01.代码如下: package TIANPAN; class Book { // 定义一个新的类 String title; // 书的名字 double price; // 书的价格 public ...
- Windows Mysql Server重启, log-bin路径配置
Windows Mysql Server重启, log-bin路径配置 分类: mysql数据库2014-03-16 14:49 1313人阅读 评论(0) 收藏 举报 Mysqlmysql serv ...
- MyEclipse插件github安装使用
MyEclipse插件github安装使用 网络上的介绍一堆堆的,但是自己尝试了下,发现问题很多,就动手做个教程. 大纲 1.git客户端安装 2.ssh配置 3.egit安装配置 4.参考资料 ...
- QQ企业通----类库的设计----UDPSocket组件等
知识点: IPEndPoint 将网络端点表示为 IP 地址和端口号. UdpClient 提供用户数据报 (UDP) 网络服务. UdpClient对象.Close 关闭 UDP 连接. ...