• Codeforces 271D


    传送门

    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".

    9813017 2015-02-13 06:13:52 njczy2010 271D - Good Substrings GNU C++ Accepted 248 ms 53000 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
    ",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
    ",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
    ");
    114     ccount=-1;
    115     check(0,0);
    116 }
    117 
    118 void out()
    119 {
    120     printf("%d
    ",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 }
  • 相关阅读:
    Linux 常用命令-01
    神经网络工具箱torch.nn -05
    Autograd与计算图-04
    Tensor的索引与变形-03
    Tensor数据类型-00
    Tensor的创建与维度查看-01
    转载:网友对rootfs的理解
    【转载】linux启动流程分析
    windows7 自带计算器bug
    网上同仁对linux文件系统的剖析
  • 原文地址:https://www.cnblogs.com/njczy2010/p/4289951.html
Copyright © 2020-2023  润新知