zoj3228(AC自动机进阶)

Searching the String

Time Limit: 7 Seconds       Memory Limit: 129872 KB

Little jay really hates to deal with string. But moondy likes it very much, and she's so mischievous that she often gives jay some dull problems related to string. And one day, moondy gave jay another problem, poor jay finally broke out and cried, " Who can help me? I'll bg him! "

So what is the problem this time?

First, moondy gave jay a very long string A. Then she gave him a sequence of very short substrings, and asked him to find how many times each substring appeared in string A. What's more, she would denote whether or not founded appearances of this substring are allowed to overlap.

At first, jay just read string A from begin to end to search all appearances of each given substring. But he soon felt exhausted and couldn't go on any more, so he gave up and broke out this time.

I know you're a good guy and will help with jay even without bg, won't you?

Input

Input consists of multiple cases( <= 20 ) and terminates with end of file.

For each case, the first line contains string A ( length <= 10^5 ). The second line contains an integer N ( N <= 10^5 ), which denotes the number of queries. The next N lines, each with an integer type and a string a ( length <= 6 ), type = 0 denotes substring a is allowed to overlap and type = 1 denotes not. Note that all input characters are lowercase.

There is a blank line between two consecutive cases.

Output

For each case, output the case number first ( based on 1 , see Samples ).

Then for each query, output an integer in a single line denoting the maximum times you can find the substring under certain rules.

Output an empty line after each case.

Sample Input

ab
2
0 ab
1 ab

abababac
2
0 aba
1 aba

abcdefghijklmnopqrstuvwxyz
3
0 abc
1 def
1 jmn

Sample Output

Case 1
1
1

Case 2
3
2

Case 3
1
1
0
这个题就比较难了。想了好久,因为它是分两种情况,即可重叠和不可重叠,可重叠的很简单了。正常的AC自动机就是搜索完回溯的,不可重叠我们用到的办法是,上次串的位置,只要加上串的长度小于当前位置,即可计算。而且,注意和标准的AC自动机模板不同,比如第一组数据,正常建树的时候第二个ab会覆盖第一个ab,所以在结构体里直接加入结果数组vis1,2,然后处理主串的时候直接更改vis值,而不是像其他一样新建ans数组。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define mem(a) memset((a),0,sizeof(a))
using namespace std;
const int N=26;
struct node
{
    int num;
    node *fail;
    node *next[N];
    int len,tmp;
    int vis[2];
    node()
    {
        fail=NULL;
        num=0;
        len=0;
        tmp=-1;
        for(int i=0; i<N; i++)
            next[i]=NULL;
        vis[0]=vis[1]=0;
    }
}*ac[600005];
int head,tail;
node *root;
char str[100005][10];
int c[100005];
char S[100005];
int *y[100005];
void build(char *s,int pos)
{
    int len=strlen(s),tmp;
    node *p=root;
    for(int i=0; i<len; i++)
    {
        tmp=s[i]-'a';
        if(p->next[tmp]==NULL)
            p->next[tmp]=new node();
        p=p->next[tmp];
    }
    p->num=pos;
    p->len=len;
    p->tmp=-1;
}
void get_fail()
{
    ac[tail++]=root;
    while(head!=tail)
    {
        node *p=ac[head++];
        node *tmp=NULL;
        for(int i=0; i<N; i++)
        {
            if(p->next[i]!=NULL)
            {
                if(p==root)
                    p->next[i]->fail=root;
                else
                {
                    tmp=p->fail;
                    while(tmp!=NULL)
                    {
                        if(tmp->next[i]!=NULL)
                        {
                            p->next[i]->fail=tmp->next[i];
                            break;
                        }
                        tmp=tmp->fail;
                    }
                    if(tmp==NULL)
                        p->next[i]->fail=root;
                }
                ac[tail++]=p->next[i];
            }
        }
    }
}
int ans0[100005],ans1[100005];
void query(char *str)
{
    node *p=root;
    int tmp,len=strlen(str);
    for(int i=0; i<len; i++)
    {
        tmp=str[i]-'a';
        while(p->next[tmp]==NULL&&p!=root)
            p=p->fail;
        p=p->next[tmp];
        if(p==NULL) p=root;
        node *exp=p;
        while(exp!=root)
        {
            if(exp->num!=0)
            {
                exp->vis[0]++;
                if(exp->tmp==-1||exp->tmp+exp->len<=i)
                {
                    exp->vis[1]++;
                    exp->tmp=i;
                }
            }
            exp=exp->fail;
        }
    }
}
int get_ans(char *s,int flag)
{
    int i=0,j;
    node *p=root;
    while(s[i])
    {
        j=s[i++]-'a';
        p=p->next[j];
    }
    return p->vis[flag];
}
void del(node *root)
{
    int i;
    if(root!=NULL)
    {
        for(i=0; i<26; i++)
        {
            if(root->next[i]!=NULL)
                del(root->next[i]);
        }
        free(root);
    }
}
int main()
{
    int n,con=1;
    while(~scanf("%s",S))
    {
        scanf("%d",&n);
        root=new node();
        head=tail=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d %s",&c[i],str[i]);
            build(str[i],i);
        }
        mem(ans0);
        mem(ans1);
        get_fail();
        query(S);
        printf("Case %d\n",con++);
        for(int i=1; i<=n; i++)
            printf("%d\n",get_ans(str[i],c[i]));
        printf("\n");
        del(root);
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是ZOJ1626的C++ AC代码,使用了旋转卡壳算法: ```c++ #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #define MAXN 100010 #define eps 1e-8 #define INF 1e20 using namespace std; struct point { double x,y; friend point operator -(point a,point b) { point res; res.x=a.x-b.x; res.y=a.y-b.y; return res; } friend bool operator <(point a,point b) { if(fabs(a.x-b.x)<eps) return a.y<b.y; return a.x<b.x; } friend double operator *(point a,point b) { return a.x*b.y-a.y*b.x; } friend double dis(point a,point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } }a[MAXN],b[MAXN],st[MAXN]; int n; double ans=INF; int cmp(point a,point b) { double tmp=(a-b)*(a[1]-b); if(fabs(tmp)<eps) return dis(a,a[1])-dis(b,a[1])<0; return tmp>0; } int main() { while(~scanf("%d",&n) && n) { for(int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y); sort(a+1,a+n+1); int tot=0; for(int i=1;i<=n;i++) { while(tot>=2 && (st[tot]-st[tot-1])*(a[i]-st[tot])<0) tot--; st[++tot]=a[i]; } int k=tot; for(int i=n-1;i>=1;i--) { while(tot>k && (st[tot]-st[tot-1])*(a[i]-st[tot])<0) tot--; st[++tot]=a[i]; } tot--; for(int i=1;i<=tot;i++) b[i]=st[i]; int tmp=1; for(int i=2;i<=tot;i++) if(b[i].y<b[tmp].y) tmp=i; swap(b[1],b[tmp]); sort(b+2,b+tot+1,cmp); st[1]=b[1]; st[2]=b[2]; k=2; for(int i=3;i<=tot;i++) { while(k>1 && (st[k]-st[k-1])*(b[i]-st[k])<=0) k--; st[++k]=b[i]; } double ans=0; if(k==2) ans=dis(st[1],st[2]); else { st[k+1]=st[1]; for(int i=1;i<=k;i++) for(int j=1;j<=k;j++) ans=max(ans,dis(st[i],st[j])); } printf("%.2lf\n",ans/2); } return 0; } ``` 其中,结构体 `point` 表示二维平面上的一个点,包含了点的坐标和一些基本操作。函数 `cmp` 是旋转卡壳算法中的比较函数,按照点到起点的极角从小到大排序。在主函数中,先使用 Graham 扫描法求出点集的凸包,然后按照旋转卡壳的步骤,求出凸包上的最远点对距离作为最小直径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值