字符串中子序列出现次数(dp)
躲藏
链接:https://ac.nowcoder.com/acm/problem/15669
来源:牛客网
题目描述
Cwbc藏在多个不区分大小写的字符串中。
好奇的XHRlyb想知道,在每个字符串中Cwbc作为子序列分别出现了多少次。
由于Cwbc可能出现的次数过多,你只需要输出每个答案对2000120420010122取模后的结果。
聪明的你在仔细阅读题目后,一定可以顺利的解决这个问题!
输入描述:
输入数据有多行,每行有一个字符串。
输出描述:
输出数据应有多行,每行表示一个答案取模后的结果。
输入
Cwbc
输出
1
说明
Cwbc作为子序列仅出现了1次。
输入
acdcecfwgwhwibjbkblcmcnco
输出
81
说明
Cwbc作为子序列出现了34=81次。
备注:
每行字符串长度不超过2×105,字符串总长度不超过106。
f[4] = (f[4] + (s[i] ==′ c′)∗f[3]) % Mod
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const LL MOD=;
const int maxn=1e5+;
using namespace std; string str1;
string str2="cwbc";
LL dp[]; int main()
{ while(cin>>str1)
{
memset(dp,,sizeof(dp));
for(int i=;i<=str1.size();i++)
{
str1[i-]=tolower(str1[i-]);
for(int j=str2.size();j>=;j--)
{
dp[j]=(dp[j]+(str1[i-]==str2[j-])*(j==?:dp[j-]))%MOD;
}
}
cout<<dp[str2.size()]<<endl;;
} return ;
}
一道类似题
I love you
链接:https://ac.nowcoder.com/acm/contest/3947/I
来源:牛客网
题目描述
此时相望不相闻,愿逐月华流照君。
一纸情书,到底蕴含了多少倍的爱情呢?
I love you, not only for what you are, but for what I am when I am with you.
输入描述:
共一行:一封若干个字符的情书(大小写不敏感)。
情书不会超过684594个字符(大写、小写字母)。
输出描述:
共一行:包含一个整数,即iloveyou在情书中作为子序列出现的次数。
由于答案可能很大,请输出对20010905取模后的值。
输入
IloveyouNotonlyforwhatyouareButforwhatIamWhenIamwithyouIloveyouNotonlyforwhatYouhavemadeofyourselfButforwhatYouaremakingofme
输出
2864
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <ctime>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const LL MOD=;
const double PI = acos(-);
const double eps =1e-;
#define Bug cout<<"---------------------"<<endl
const int maxn=1e5+;
using namespace std; string str1;
string str2="iloveyou";
LL dp[]; int main()
{
#ifdef DEBUG
freopen("sample.txt","r",stdin);
#endif
// ios_base::sync_with_stdio(false);
// cin.tie(NULL); while(cin>>str1)
{
memset(dp,,sizeof(dp));
for(int i=;i<=str1.size();i++)
{
// if(!((str1[i-1]>='A'&&str1[i-1]<='Z')||(str1[i-1]>='a'&&str1[i-1]<='z'))) continue;
str1[i-]=tolower(str1[i-]);
for(int j=str2.size();j>=;j--)
{
dp[j]=(dp[j]+(str1[i-]==str2[j-])*(j==?:dp[j-]))%MOD;
}
}
cout<<dp[str2.size()]<<endl;;
} return ;
}
队友写的:
//MADE BY Y_is_sunshine;
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set> #define INF 0x3f3f3f3f
#define MAXN 700005 typedef long long ll; const ll mod = ;
const double PI = acos(-); using namespace std; int N, M, K; ll dp[][MAXN]; int main(void)
{ string s;
cin >> s;
//transform(s.begin(), s.end(), s.begin(), tolower); s.insert(s.begin(), ' ');
//cout << s << '\n'; string ss = " iloveyou"; N = s.size();
M = ss.size(); for (int j = ; j <= N; j++) {
if (s[j] >= 'A' && s[j] <= 'Z')
s[j] += ;
dp[][j] = ;
}
for (int i = ; i <= M; i++) {
for (int j = ; j < N + ; j++) {
if (s[j - ] == ss[i - ]) {
dp[i][j] = dp[i][j - ] + dp[i - ][j - ];
}
else {
dp[i][j] = dp[i][j - ];
}
dp[i][j] %= mod;
}
} cout << dp[M][N] << '\n'; return ;
}
-
字符串中子序列出现次数(dp)的更多相关文章
- BZOJ 2754([SCOI2012]喵喵叫的星球-统计序列的后缀阵列中子序列出现次数)
2754: [SCOI2012]喵喵叫的星球 Time Limit: 20 Sec Memory Limit: 128 MB Submit: 805 Solved: 380 [id=2754&qu ...
- Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划
Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...
- 区间和序列上的dp
区间上的dp状态设计最基本的形式: \(F[i]\)表示以i结尾的最优值或方案数. \(F[i][k]\)表示以i结尾附加信息为k的最优值或方案数. 当然可以有多维附加信息. 转移的话往往是枚举上一个 ...
- java小练习--获取abc字符串在整个字符串中出现的次数
在下面一行字符串中获取abc字符串在整个字符串中出现的次数. "wabcerabctyabcuiabcabcqq" 思路:使用indexOf和substring(); 源码如下: ...
- str_repeat() 函数把字符串重复指定的次数。
str_repeat() 函数把字符串重复指定的次数. str_repeat(string,repeat) 参数 描述 string 必需.规定要重复的字符串. repeat 必需.规定字符串将被重复 ...
- 已知一个字符串S 以及长度为n的字符数组a,编写一个函数,统计a中每个字符在字符串中的出现次数
import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/6 21:04 * @description ...
- Java实现统计某字符串在另一个字符串中出现的次数
面试时会经常考这样的题目,估计也不让使用正则表达式.还好这个算法还算简单,不过在草稿纸上写难免会出现运行异常,好吧,面试官赢了,乃们屌丝就实实在在的把代码码出来吧. 谢谢“心扉”对我代码bug的纠正, ...
- mysql 统计一个字符在字符串中出现的次数
CREATE FUNCTION `str_pcount`(str varchar(255),p varchar(255)) RETURNS int(11)BEGIN #统计一个字符在字符串中出 ...
- python取一个字符串中最多出现次数的词
#-*- coding:utf-8 -*- #取一个字符串中最多出现次数的词 import re from collections import Counter my_str = "&quo ...
随机推荐
- 查看linux硬件的信息
cpu: cat /proc/cpuinfo 内存: cat /proc/meminfo 查看内存使用情况: free -m -m指以M的单位显示 查看硬盘使用情况: df -h ...
- Java 逆序打印链表
递归 package cookie; public class PrintListReversal { public void reversalOut(Node head) { if (head != ...
- kafka cmd with ssl
set PATH=C:\Program Files\Java\jdk1.8.0_201\bin;@call kafka-consumer-groups.bat --bootstrap-server l ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 数据结构
C/C++ 数组允许定义可存储相同类型数据项的变量,但是结构是 C++ 中另一种用户自定义的可用的数据类型,它允许存储不同类型的数据项. 结构用于表示一条记录,假设想要跟踪图书馆中书本的动态,可能需要 ...
- Elasticsearch 更新文档
章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...
- TRUNC()函数——oracle
使用trunc()函数获取不同的日期: select trunc(sysdate) from dual; --今天的日期 select trunc(sysdate,'dd') from dual; - ...
- 心形java和C语言2019/10/17
在网上无意中看到这个代码,学习了一下心形函数的知识:http://mathworld.wolfram.com/HeartCurve.html package dada; /** * 了解Heart C ...
- 微信公众号开发之内网映射外网natapp安装(一)
一,为什么使用natapp 1,在进行微信公众号开发时,我们需要搭建网站,并且随时都有可能修改网站内容进行调试.如果能够将内网ip映射到外网上,将大大方便我们的调试.每次发布只需eclipse运行应用 ...
- stm32h7 hal 库的学习
stm32h7xx_hal_conf.h 中需要注意的几个地方: HSE_VALUE 这个外接晶振的频率 TICK_INT_PRIORITY 这个 tick 的中断优先级,因为 HAL_DELAY 这 ...
- 图片分割之GrabCut算法、分水岭算法
https://www.cnblogs.com/zyly/p/9392881.html 所谓图像分割指的是根据灰度.颜色.纹理和形状等特征把图像划分成若干互不交迭的区域,并使这些特征在同一区域内呈现出 ...