Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求指点。。

Posted by 331895686 at 2011-12-02 08:46:59 on Problem 1204
无限WA。。。求大牛指点!
#include<stdio.h>
#include<string.h>
#include<queue>
#include<new>
using namespace std;
const int N = 1010;
struct data 
{
	data *child[26];
	int sign;
	data *fail;
};
int ct=0;
data *head;
int n,m;
const int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
char Map[N][N];
int X[N],Y[N],W[N],len[N];
char s[2*N];
data *newnode()
{
    data *p=new(data);
    p->sign=0;
    for(int i=0;i<26;i++)
        p->child[i]=NULL;
    p->fail=head;
    return p;
}
int insert(int sig,char s[])
{
    data *h=head;
	int i;
    for(i=0;s[i];i++)
    {
        int r=s[i]-'A';
        if(!h->child[r])
            h->child[r]=newnode();
        h=h->child[r];
    }
    h->sign=sig;
	return i;
}

data *updata(data *h,int k)
{
    if(h->child[k])
        return h->child[k];
    else
    {
        if(h==head) 
             return head;
        else return updata(h->fail,k);
    }
}
void creatauto()
{
    queue<data*> q;
    q.push(head);
    while(!q.empty())
    {
        data *h=q.front();
        q.pop();
        for(int i=0;i<26;i++)
        {
            if(h->child[i])
            {
                q.push(h->child[i]);
                if(h==head) h->child[i]->fail=head;
                else 
                    h->child[i]->fail=updata(h->fail,i);
            }
        }
    }
}	
void finds(int row,int col,int d)
{
	int x=dir[d][0];
	int y=dir[d][1];
	int i,j;
	data *h=head;
	int sig;
	for(i=row,j=col;i>=0&&j>=0&&i<n&&j<m;i+=x,j+=y)
	{
		int r=Map[i][j]-'A';
        if(h->child[r]) h=h->child[r];
        else 
        {
            while(!h->child[r]&&h!=head) h=h->fail;
            if(!h->child[r]) h=head;
            else h=h->child[r];
        }
        data *hh=h;
        while(hh!=head&&hh->sign!=-1)
        {
			if(hh->sign)
			{
				int g=hh->sign-1;
				if(X[g]==-1)
				{
					X[g]=i-(len[g]-1)*x;
           			Y[g]=j-(len[g]-1)*y;
           			W[g]=d;
				}
			}
			hh->sign=-1;
			hh=hh->fail;
        }
	}
}
int main()
{
	int k;
	scanf("%d%d%d",&n,&m,&k);
	for(int i=0;i<n;i++)
		scanf("%s",Map[i]);
	memset(X,-1,sizeof(X));
	head=newnode();
	head->fail=head;
	for(int i=0;i<k;i++)
	{
		scanf("%s",s);
		len[i]=insert(i+1,s);
	}
	creatauto();
	for(int i=0;i<m;i++)
	{
		finds(0,i,4);
		finds(0,i,5);
		finds(n-1,i,0);
		finds(n-1,i,1);
	}
	for(int i=0;i<n;i++)
	{
		finds(i,0,2);
		finds(i,0,3);
		finds(i,m-1,6);
		finds(i,m-1,7);
	}	
	for(int i=0;i<k;i++)
		printf("%d %d %c\n",X[i],Y[i],W[i]+'A');
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator