统计一个只由大写字母构成的字符串的,子串数目,这里指的是子串不是子序列,可以不连续,请注意

然后我按照计数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的更多相关文章

  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. USACO 2019 December Contest 随记

    Forewords 今年 USACO 的比赛变化挺大的,有部分分了,而且不再是固定十个点了(部分分只说这几个点满足这几个性质,以为十个点的我还高兴了一会,一提交,...),除此之外居然赛后还排名了.这 ...

  2. 用java实现输出英文小说飘中出现次数最多的前N个单词(附:使用文件读写)

    本文参考于:https://blog.csdn.net/u014204432/article/details/40348839 一.题目 输出单个文件(<飘> 英文版)中的前 N 个最常出 ...

  3. 8张图片掌握JS原型链

  4. mac brew redis

    1.搜索brew search redis //出现如下 ==> Searching local taps... hiredis redis redis-leveldb redis@2.8 re ...

  5. IDEA配置Scala开发环境

    1.下载scala sdk 到页面 https://www.scala-lang.org/download/2.10.5.html 下载sdk,大数据平台目前的scala版本是2.10.5,下载后解压 ...

  6. Docker基本使用运行ngix镜像

    docker pull 项目名 会从docker默认的仓库去拉去项目,如果是docker pull 项目名 地址 会从给定地址拉去镜像 docker run image名字  运行镜像 docker架 ...

  7. Ubuntu用sudo apt-get update出错:E: Problem executing scripts APT::Update::Post-Invoke-Success

    Ubuntu用sudo apt-get update出错:   E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /u ...

  8. mysq8设置编码utf8

    设置mysql默认编码utf8 以及其他配置 系统:centos7 vi /etc/my.cnf #红色部分如果以存在则在他的下方添加 [mysql] default-character-set=ut ...

  9. 基于LAMP实现后台活动发布和前端扫码签到系统

    目的 无论是公司.学校和社会团体,都会举办各式各样的活动,比如运动会.部门会议.项目会议.野炊.团建等.作为团队管理者来讲,当然希望能够把这类活动转移到线上形成完整的系统,类似于电子流的形式.本文以学 ...

  10. 强大的promise

    这个玩意叫做普罗米修斯,希腊神话的盗火英雄 promise只用来包装异步函数,同步的会搞乱执行顺序,生产BUG // 如何使用 function pro(){ return new Promise(f ...