POJ 1204 【AC自动机】

开始wa掉了,360行的代码找错真难受!!!居然是一个地方多减了一个1……

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;

#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif

#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a)         for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a)        for( int i = (a)-1 ; i >= 0 ; i --)
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define pb              push_back
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define read            freopen("1204.in","r",stdin)
#define write           freopen("out.txt","w",stdout)

const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 1e-10;
const double pi = acos(-1.0);
const int maxn = 1001;
const int maxc = 26;
const int maxz = 1000001;
const int head = 0;

int n,m,w;
string sd[maxn];
string s[maxn];

struct zz
{
    int to[26];
    int fail;
    int id;
}zx[maxz];

bool vis[maxz];
int use;
int fx[maxn];
int fy[maxn];
char cf[maxn];

int get()
{
    use++;
    zx[use].id = 0;
    zx[use].fail = 0;
    MM(zx[use].to,-1);
    return use;
}
void ac();

void insert(int id,string& p)
{
    int now = head;
    int c;
    for(int i=0;i<p.length();i++)
    {
        c = p[i] - 'A';
        if(zx[now].to[c] == -1)
        {
            zx[now].to[c] = get();
            now = zx[now].to[c];
        }
        else
        {
            now = zx[now].to[c];

        }
    }
    zx[now].id = id;
    return ;
}

void ac()
{
    queue<int>q;
    CL(q);
    q.push(0);
    int now,to,temp;
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        for(int i=0;i<maxc;i++)
        {
            if(zx[now].to[i]!=-1)
            {
                to = zx[now].to[i];
                temp = now;
                q.push(to);
                while(temp)
                {
                    temp = zx[temp].fail;
                    if(zx[temp].to[i] != -1)
                    {
                        zx[to].fail = zx[temp].to[i];
                        break;
                    }
                }
            }
        }
    }
    return ;
}

void find(int x,int y,char fc,int now)
{
    while(now && !vis[now])
    {
        vis[now] = true;
        if(zx[now].id)
        {
            int id = zx[now].id;
            int low = s[id].length()-1;
            int nowx,nowy;
            if(fc == 'C')
            {
                nowx = x;
                nowy = y - low;
            }
            else if(fc == 'G')
            {
                nowx = x;
                nowy = y + low;
            }
            else if(fc == 'A')
            {
                nowx = x + low;
                nowy = y;
            }
            else if(fc == 'E')
            {
                nowx = x - low;
                nowy = y;
            }
            else if(fc == 'B')
            {
                nowx = x + low;
                nowy = y - low;
            }
            else if(fc == 'F')
            {
                nowx = x - low;
                nowy = y + low;
            }
            else if(fc == 'D')
            {
                nowx = x - low;
                nowy = y - low;
            }
            else if(fc == 'H')
            {
                nowx = x + low;
                nowy = y + low;
            }
            fx[id] = nowx;
            fy[id] = nowy;
            cf[id] = fc;
        }
        now = zx[now].fail;
    }
    return ;
}

void ak(int x,int y,char fc,string& p)
{
    int now=head;
    int temp;
    int c;
    for(int i=0;i<p.length();i++)
    {
        c = p[i]-'A';
        if(zx[now].to[c] != -1)
        {
            now = zx[now].to[c];
        }
        else
        {
            temp = now;
            while(now)
            {
                now = zx[now].fail;
                if(zx[now].to[c] != -1)
                {
                    now = zx[now].to[c];
                    break;
                }
            }
            zx[temp].to[c] = now;
        }
        if(fc == 'C')
        {
            find(x,y+i,fc,now);
        }
        else if(fc == 'G')
        {
            find(x,y-i,fc,now);
        }
        else if(fc == 'A')
        {
            find(x-i,y,fc,now);
        }
        else if(fc == 'E')
        {
            find(x+i,y,fc,now);
        }
        else if(fc == 'B')
        {
            find(x-i,y+i,fc,now);
        }
        else if(fc == 'F')
        {
            find(x+i,y-i,fc,now);
        }
        else if(fc == 'D')
        {
            find(x+i,y+i,fc,now);
        }
        else if(fc == 'H')
        {
            find(x-i,y-i,fc,now);
        }
    }
    return ;
}

void start()
{
    ac();

    string temp;
    char c;
    for(int i=0;i<n;i++)
    {
        temp = sd[i];
        c = 'C';
        ak(i,0,c,temp);
        reverse(temp.begin(),temp.end());
        c = 'G';
        ak(i,m-1,c,temp);
    }

    for(int j=0;j<m;j++)
    {
        temp = "";
        for(int i=0;i<n;i++)
        {
            temp += sd[i][j];
        }
        c = 'E';
        ak(0,j,c,temp);
        c = 'A';
        reverse(temp.begin(),temp.end());
        ak(n-1,j,c,temp);
    }


    for(int i=0;i<n;i++)
    {
        temp = "";
        for(int j=0;j<=i && j<m;j++)
        {
            temp += sd[i-j][j];
        }
        c = 'B';
        ak(i,0,c,temp);
        reverse(temp.begin(),temp.end());
        c = 'F';
        ak(i-min(m-1,i),min(m-1,i),c,temp);
    }

    for(int j=0;j<m;j++)
    {
        temp = "";
        for(int i=n-1;i>=0 && j+n-1-i<=m-1;i--)
        {
            temp += sd[i][j+n-1-i];
        }
        c = 'B';
        ak(n-1,j,c,temp);
        reverse(temp.begin(),temp.end());
        c = 'F';
        ak(max(0,j+n-m),j+n-1-max(0,j+n-m),c,temp);
    }
    for(int i=0;i<n;i++)
    {
        temp = "";
        for(int j=0;j<=n-1-i && j<m ;j++)
        {
            temp += sd[i+j][j];
        }
        c = 'D';
        ak(i,0,c,temp);
        reverse(temp.begin(),temp.end());
        c = 'H';
        ak(i+min(n-1-i,m-1),min(n-1-i,m-1),c,temp);
    }

    for(int j=0;j<m;j++)
    {
        temp = "";
        for(int i=0;i<n && j+i<m;i++)
        {
            temp += sd[i][j+i];
        }
        c = 'D';
        ak(0,j,c,temp);
        reverse(temp.begin(),temp.end());
        c = 'H';
        ak(min(n-1,m-j-1),j+min(n-1,m-j-1),c,temp);
    }
    return ;
}


int main()
{
    cin>>n>>m>>w;
    use = -1;
    get();
    MM(vis,false);
    for(int i=0;i<n;i++)
    {
        cin>>sd[i];
    }
    for(int i=1;i<=w;i++)
    {
        cin>>s[i];
        insert(i,s[i]);
    }
    start();
    for(int i=1;i<=w;i++)
    {
        cout<<fx[i]<<" "<<fy[i]<<" "<<cf[i]<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值