Codeforces 271D - Good Substrings [字典树]

本文探讨了如何在给定的英文字符串中,找出包含最多k个特定字符(标记为'bad')的子串,并计算这些子串的唯一数量。通过输入字符串、标记好字符的二进制字符串以及最大允许的'bad'字符数量,算法能够高效地计算出符合条件的子串总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

D. Good Substrings
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.

A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string slsl + 1...sr.

The substring s[l...r] is good, if among the letters sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).

Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].

Input

The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters.

The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on.

The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.

Output

Print a single integer — the number of distinct good substrings of string s.

Sample test(s)
Input
ababab
01000000000000000000000000
1
Output
5
Input
acbacbacaa
00000000000000000000000000
2
Output
8
Note

In the first example there are following good substrings: "a", "ab", "b", "ba", "bab".

In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".

 

98130172015-02-13 06:13:52njczy2010 271D - Good Substrings GNU C++Accepted 248 ms53000 KB 

 

 

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 #include<set>
 10 #include<stack>
 11 #include<string>
 12 
 13 #define N 2250005
 14 #define M 1505
 15 #define mod 10000007
 16 //#define p 10000007
 17 #define mod2 1000000000
 18 #define ll long long
 19 #define LL long long
 20 #define eps 1e-6
 21 #define inf 100000000
 22 #define maxi(a,b) (a)>(b)? (a) : (b)
 23 #define mini(a,b) (a)<(b)? (a) : (b)
 24 
 25 using namespace std;
 26 
 27 int n;
 28 int T;
 29 int flag;
 30 int ccount;
 31 char ss[M];
 32 char f[30];
 33 
 34 typedef struct
 35 {
 36     int v;
 37     vector<int>nt;
 38 }PP;
 39 
 40 PP p[N];
 41 int le;
 42 
 43 int k;
 44 int tot;
 45 
 46 void insert(char s[])
 47 {
 48     int l=strlen(s);
 49     int i;
 50     int ff;
 51     int st=0;
 52     int y;
 53     vector<int>::iterator it;
 54     for(i=0;i<l;i++){
 55         ff=0;
 56         for(it=p[st].nt.begin();it!=p[st].nt.end();it++){
 57             y=*it;
 58             if(p[y].v==s[i]-'a'){
 59                 ff=1;
 60                 st=y;
 61                 break;
 62             }
 63         }
 64         if(ff==0){
 65             p[st].nt.push_back(tot);
 66             p[tot].v=s[i]-'a';
 67             p[tot].nt.clear();
 68             st=tot;
 69             tot++;
 70         }
 71     }
 72 }
 73 
 74 void ini()
 75 {
 76     ccount=0;
 77     int i;
 78     scanf("%s",f);
 79     scanf("%d",&k);
 80     flag=1;
 81     p[0].nt.clear();
 82     p[0].v=-1;
 83     tot=1;
 84     scanf("%d",&n);
 85     le=strlen(ss);
 86     for(i=0;i<le;i++){
 87         insert(ss+i);
 88     }
 89 }
 90 
 91 void check(int st,int now)
 92 {
 93     //printf(" st=%d v=%d now=%d\n",st,p[st].v,now);
 94     int y;
 95     if(now>k) return;
 96     ccount++;
 97     vector<int>::iterator it;
 98     for(it=p[st].nt.begin();it!=p[st].nt.end();it++)
 99     {
100         y=*it;
101         //printf("  st=%d y=%d f=%c\n",st,y,f[ p[y].v ]);
102         if(f[ p[y].v ]=='1'){
103             check(y,now);
104         }
105         else{
106             check(y,now+1);
107         }
108     }
109 }
110 
111 void solve()
112 {
113     //printf("solve\n");
114     ccount=-1;
115     check(0,0);
116 }
117 
118 void out()
119 {
120     printf("%d\n",ccount);
121 }
122 
123 int main()
124 {
125     //freopen("data.in","r",stdin);
126     //freopen("data.out","w",stdout);
127     //scanf("%d",&T);
128     //for(int ccnt=1;ccnt<=T;ccnt++)
129     //while(T--)
130     //scanf("%d%d",&n,&m);
131     while(scanf("%s",ss)!=EOF)
132     {
133         ini();
134         solve();
135         out();
136     }
137     return 0;
138 }

 

转载于:https://www.cnblogs.com/njczy2010/p/4289951.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值