ZOJ Monthly, August 2012 3643 Keep Deleting

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3643

题意:给出A串和B串,删除B串中的A串。

思路:栈+暴力,strcmp函数比较的时候,如果不相等就会马上退出,这样最坏的时间复杂度是O(maxn/256*(1+256)*256/2)。

ps:不知道官方解题是怎么样弄的。

View Code
#include<stdio.h>
#include<string.h>
#define maxn 512005
char as[maxn],bs[2000],st[maxn];
int ans,tp;
int main()
{
    int i,k;
    while(scanf("%s%s",bs,as)==2){
        ans = 0;
        tp = 1;
        k = strlen(bs);
        for(i = 0; as[i]; ++ i){
            st[tp-1] = as[i];
            if(tp>=k){
                st[tp]=0;
                if(strcmp(st+tp-k,bs)==0){
                    tp -= k;
                    ans++;
                }
            }
            ++tp;
        }
        printf("%d\n",ans);
    }
    return 0;
}

posted on 2012-08-26 21:41  aigoruan  阅读(196)  评论(0)    收藏  举报

导航