题目:给两个字符串a、b,问从a中删去若干字符后最多可以得到多少个b串的重复串(bb...b的形式,b的长度不超过100),其中a串是由一个长度不超过100的字符串s重复k次得到的
思路: 暴力匹配a和b,由于s,b的长度都不超过100,标记每次匹配后a串指针的位置对len(s)的模,那么最多有100种标记,每种标记最多导致a串指针移动100*100位,那么在a串的前1e6个字符,一定可以得到重复的标记,而重复的标记之间就是循环节,跳过中间的若干循环节,处理最后剩余的a串字符(一定小于1e6个),继续暴力匹配下就行了。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | /* ******************************************************************************** */#include <iostream>                                                                 //#include <cstdio>                                                                   //#include <cmath>                                                                    //#include <cstdlib>                                                                  //#include <cstring>                                                                  //#include <vector>                                                                   //#include <ctime>                                                                    //#include <deque>                                                                    //#include <queue>                                                                    //#include <algorithm>                                                                //#include <map>                                                                      //usingnamespacestd;                                                                //                                                                                    //#define pb push_back                                                                //#define mp make_pair                                                                //#define X first                                                                     //#define Y second                                                                    //#define all(a) (a).begin(), (a).end()                                               //#define foreach(a, i) for (typeof(a.begin()) i = a.begin(); i != a.end(); ++ i)     //#define fill(a, x) memset(a, x, sizeof(a))                                          //                                                                                    //voidRI(vector<int>&a,intn){a.resize(n);for(inti=0;i<n;i++)scanf("%d",&a[i]);}    //voidRI(){}voidRI(int&X){scanf("%d",&X);}template<typename...R>                    //voidRI(int&f,R&...r){RI(f);RI(r...);}voidRI(int*p,int*q){intd=p<q?1:-1;          //while(p!=q){scanf("%d",p);p+=d;}}voidprint(){cout<<endl;}template<typenameT>      //voidprint(constT t){cout<<t<<endl;}template<typenameF,typename...R>              //voidprint(constF f,constR...r){cout<<f<<", ";print(r...);}template<typenameT>   //voidprint(T*p, T*q){intd=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}   //                                                                                    //typedefpair<int, int> pii;                                                         //typedeflonglongll;                                                               //typedefunsigned longlongull;                                                     //                                                                                    //template<typenameT>boolumax(T&a, constT&b){returnb>a?false:(a=b,true);}         //template<typenameT>boolumin(T&a, constT&b){returnb<a?false:(a=b,true);}         //template<typenameT>                                                                //voidV2A(T a[],constvector<T>&b){for(inti=0;i<b.size();i++)a[i]=b[i];}            //template<typenameT>                                                                //voidA2V(vector<T>&a,constT b[]){for(inti=0;i<a.size();i++)a[i]=b[i];}            //                                                                                    ///* -------------------------------------------------------------------------------- */                                                                                    //constintmaxn = 1234567;chars[maxn], a[123], c[123];intlena, lenc, b, d, ta;intbuf[123], mark[123];voidinit() {    char*ps = s;    for(inti = 0; i < b; i ++) {        for(intj = 0; j < lena; j ++) {            *ps ++ = a[j];            if(ps - s == ta || ps - s > 1111111) return;        }    }}intwork(int);voidsolve(int&cc, intpp) {    if(pp == ta - 1) return;    intrest = ta - pp - 1, cnt = rest / lena;    if(rest % lena) cnt ++;    ta = cnt * lena;    s[ta] = 0;    cc += work(pp % lena + 1);}intwork(intstart) {    memset(mark, 0, sizeof(mark));    memset(buf, 0, sizeof(buf));    intcc = 0, nowc = 0;    for(inti = start; s[i]; i ++) {        if(s[i] == c[nowc]) nowc ++;        if(nowc == lenc) {            cc ++;            nowc = 0;            if(mark[i % lena]) {                cc += (ta - i - 1) / (i + 1 - mark[i % lena]) * (cc - buf[i % lena]);                intpp = i + (ta - i - 1) / (i + 1 - mark[i % lena]) *                            (i + 1 - mark[i % lena]);                solve(cc, pp);                returncc;            }            else{                mark[i % lena] = i + 1;                buf[i % lena] = cc;            }        }    }    returncc;}intmain() {#ifndef ONLINE_JUDGE    freopen("in.txt", "r", stdin);#endif // ONLINE_JUDGE    cin >> b >> d;    scanf("%s%s", a, c);    lena = strlen(a);    lenc = strlen(c);    ta = lena * b;    init();    cout << work(0) / d << endl;    return0;                                                                       //}                                                                                   //                                                                                    //                                                                                    //                                                                                    ///* ******************************************************************************** */ | 
原文:http://www.cnblogs.com/jklongint/p/4687970.html