CodeForces 314B. Sereja and Periods 思维+简单dp

来源:互联网 发布:facebook直播软件下载 编辑:程序博客网 时间:2024/06/07 01:59

题目


题意:给出不超过100长度的两个字符串a、c,以及两个正整数b、d(b,d<=1e7),求最大的q使得ab()(bd)q


这个题我的思考角度不大好,唉…
令dp[x]表示c串的第x字符开始匹配,匹配完一个字符串a后,能够匹配多少个整字符串c,并且nex[x]代表下次开始匹配的位置。

状态选取的好…之前一直想的是匹配完一个字符串c后消耗多少a,除了还要考虑下次匹配开始a的位置,还需要考虑b和d…

#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])#define mem(a,x)  memset(a,x,sizeof a)#define ysk(x)  (1<<(x))typedef long long ll;typedef pair<int, int> pii;const int INF =0x3f3f3f3f;const int maxn=100    ;int b,d,lenA,lenC;string a,c;int dp[maxn+10],nex[maxn+10];void cope(int x){   dp[x]=0;nex[x]=x;int use=0;   while(use<lenA)   {       if(a[use ]==c[nex[x]] )       {           if(++nex[x]==lenC)           {               dp[x]++;nex[x]=0;           }       }       use++;   }}int main(){   std::ios::sync_with_stdio(false);   while(cin>>b>>d)   {       cin>>a>>c;lenA=a.length(),lenC=c.length();       for0(i,lenC)  cope(i);       int x=0,num=0;       for0(i,b)       {           num+=dp[x];           x=nex[x];       }       num/=d;       cout<<num<<endl;   }   return 0;}
0 0
原创粉丝点击