字符串中子序列出现次数(dp)

躲藏

链接:https://ac.nowcoder.com/acm/problem/15669
来源:牛客网

题目描述

XHRlyb和她的小伙伴Cwbc在玩捉迷藏游戏。
Cwbc藏在多个不区分大小写的字符串中。
好奇的XHRlyb想知道,在每个字符串中Cwbc作为子序列分别出现了多少次。
由于Cwbc可能出现的次数过多,你只需要输出每个答案对 2000120420010122取模后的结果。
聪明的你在仔细阅读题目后,一定可以顺利的解决这个问题!

输入描述:

输入数据有多行,每行有一个字符串。

输出描述:

输出数据应有多行,每行表示一个答案取模后的结果。

示例1

输入

Cwbc

输出

1

说明

Cwbc作为子序列仅出现了1次。

示例2

输入

acdcecfwgwhwibjbkblcmcnco

输出

81

说明

Cwbc作为子序列出现了34=81次。

备注:

每行字符串长度不超过2×105,字符串总长度不超过106

f[4] = (f[4] + (s[i] ==′ c′)∗f[3]) % Mod

扫描二维码关注公众号,回复: 8718415 查看本文章

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 const LL MOD=2000120420010122;
17 const int maxn=1e5+10;
18 using namespace std;
19 
20 string str1;
21 string str2="cwbc";
22 LL dp[10];
23 
24 int main()
25 {
26     
27     while(cin>>str1)
28     {
29         memset(dp,0,sizeof(dp));
30         for(int i=1;i<=str1.size();i++)
31         {
32             str1[i-1]=tolower(str1[i-1]);
33             for(int j=str2.size();j>=1;j--)
34             {
35                 dp[j]=(dp[j]+(str1[i-1]==str2[j-1])*(j==1?1:dp[j-1]))%MOD;
36             }
37         }
38         cout<<dp[str2.size()]<<endl;;
39     }
40     
41     return 0;
42 }

一道类似题

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取模后的值。
 
示例1

输入

IloveyouNotonlyforwhatyouareButforwhatIamWhenIamwithyouIloveyouNotonlyforwhatYouhavemadeofyourselfButforwhatYouaremakingofme

输出

2864
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 #include <ctime>
14 const int INF=0x3f3f3f3f;
15 typedef long long LL;
16 const int mod=1e9+7;
17 const LL MOD=20010905;
18 const double PI = acos(-1);
19 const double eps =1e-8;
20 #define Bug cout<<"---------------------"<<endl
21 const int maxn=1e5+10;
22 using namespace std;
23 
24 string str1;
25 string str2="iloveyou";
26 LL dp[10];
27 
28 int main()
29 {
30     #ifdef DEBUG
31     freopen("sample.txt","r",stdin);
32     #endif
33 //    ios_base::sync_with_stdio(false);
34 //    cin.tie(NULL);
35     
36     while(cin>>str1)
37     {
38         memset(dp,0,sizeof(dp));
39         for(int i=1;i<=str1.size();i++)
40         {
41 //            if(!((str1[i-1]>='A'&&str1[i-1]<='Z')||(str1[i-1]>='a'&&str1[i-1]<='z'))) continue;
42             str1[i-1]=tolower(str1[i-1]);
43             for(int j=str2.size();j>=1;j--)
44             {
45                 dp[j]=(dp[j]+(str1[i-1]==str2[j-1])*(j==1?1:dp[j-1]))%MOD;
46             }
47         }
48         cout<<dp[str2.size()]<<endl;;
49     }
50     
51     return 0;
52 }

队友写的:

 1 //MADE BY Y_is_sunshine;
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <sstream>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <string>
10 #include <cmath>
11 #include <queue>
12 #include <stack>
13 #include <map>
14 #include <set>
15  
16 #define INF 0x3f3f3f3f
17 #define MAXN 700005
18  
19 typedef long long ll;
20  
21 const ll mod = 20010905;
22 const double PI = acos(-1);
23  
24 using namespace std;
25  
26 int N, M, K;
27  
28 ll dp[10][MAXN];
29  
30 int main(void)
31 {
32  
33     string s;
34     cin >> s;
35     //transform(s.begin(), s.end(), s.begin(), tolower);
36  
37     s.insert(s.begin(), ' ');
38     //cout << s << '\n';
39  
40     string ss = " iloveyou";
41  
42     N = s.size();
43     M = ss.size();
44  
45  
46     for (int j = 0; j <= N; j++) {
47         if (s[j] >= 'A' && s[j] <= 'Z')
48             s[j] += 32;
49         dp[0][j] = 1;
50     }
51     for (int i = 1; i <= M; i++) {
52         for (int j = 1; j < N + 1; j++) {
53             if (s[j - 1] == ss[i - 1]) {
54                 dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1];
55             }
56             else {
57                 dp[i][j] = dp[i][j - 1];
58             }
59             dp[i][j] %= mod;
60         }
61     }
62  
63     cout << dp[M][N] << '\n';
64 
65     return 0;
66 }

-

猜你喜欢

转载自www.cnblogs.com/jiamian/p/12210899.html