字符串中子序列出現次數(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

 

 

 

 

 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 }

 

 

 

 

-


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM