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

然后我按照计数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. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:将悬停的颜色应用在行或者单元格上

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. 首款中文渗透测试专用Linux系统—MagicBox

    1.     MagicBox的介绍 首款中文渗透测试专用Linux系统——MagicBox即将问世,中文名称:“魔方系统”,开发代号:Genesis.第一版本发布时间计划为2012年12月5日 这是 ...

  3. 初学JQuery

    JQuery是对JavaScript的封装,简化了JS代码,是主流框架的基础(VUE,EasyUI,Bootstrap) 它是2006年推出的 JQuery的优势:体积小,压缩后只有100KB左右强大 ...

  4. js 字符串 常用处理方式(检索、截取、拼接、批量替换)

    // 检索(字符串中判断是否包含某个字符) 字符串.search('检索的内容');// 返回-1,不包含: 返回非-1,包含 字符串.indexOf("待判断的内容"); // ...

  5. 导弹拦截p1020(LIS问题)

    题目描述(题目链接:https://www.luogu.org/problem/P1020) 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够 ...

  6. SSH整合出现的问题

    一 .  使用action注入service的时候,service为null 1 如果是xml配置方式,则一般是配置文件的路径问题. 2 如果是注解方式,@Resource , 则需要在applica ...

  7. java校招一些面试的题目

    数组和链表的区别 数组静态分配内存,链表动态分配内存: 数组在内存中连续,链表不一定连续: 数组元素在栈区,链表元素在堆区: 数组利用下标定位,时间复杂度为O(1),链表定位元素时间复杂度O(n) 数 ...

  8. poj 3069 Saruman's Army 贪心模拟

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18794   Accepted: 9222 D ...

  9. (十二)微信小程序实现登陆页面+登陆逻辑

    微信小程序实现登陆页面 实现上面两个页面 第一个页面 <view> <!-- 上侧部分 --> <view class="top-view"> ...

  10. 2.24 模拟赛 + DIV2 总结

    本来实在是不想打了,后来真的手痒. 晚上发现正进行DIV2然后就打了场,模拟,幸好没参加,逆风.排名2400 Codeforces Round #622 (Div. 2) A题十分钟过了 B题http ...