CodeForces 315.D Sereja and Periods

传送门:

http://codeforces.com/contest/315/problem/D

类型:

字符串

题意:

给出两个重复的字符串,告诉你重复单元和重复次数,问第二个字符串在第一个串中出现的次数(子序列)

方法:

http://blog.csdn.net/u010710717/article/details/9059403

求出串二单元每个位置开头时,在串一单元中能完整出现的次数,和经过这个单元后, 串二当前位置。

然后从0到串一重复次数模拟一遍。得到串二单元在串一中出现的完整次数

最后除以串二重复次数(取整),得到答案。

代码:

    #include <stdio.h>  
    #include <string.h>  
    #define LL __int64  
      
    char a[111], c[111] ;  
    int cnt[111], next[111];  
    int main()  
    {  
        int b,d,i,j;  
        scanf("%d%d%s%s", &b, &d, a, c) ;  
        int len1 = strlen(a);  
        int len2 = strlen(c);  
        for(i = 0;i < len2; i++)  
        {  
            int cur = i;  
            cnt[i] = 0;  
            for(j = 0;j < len1; j++)  
                if(c[cur] == a[j])  
                {  
                    cur++;  
                    if(cur == len2)  
                        cnt[i] ++ , cur = 0;  
                }  
            next[i] = cur;  
        }  
        int cur = 0;  
        LL ans = 0;  
        for(i = 0;i < b; i++)  
        {  
            ans += cnt[cur];  
            cur = next[cur];  
        }  
        printf("%I64d\n", ans/d);  
        return 0;  
    }  
View Code
原文地址:https://www.cnblogs.com/shinecheng/p/3131683.html