poj 字符串相关之1204 Word Puzzles

poj 字符串相关之1204 Word Puzzles
最好的解法网上有关于AC自动机的,我觉得夏令营就算出这样的题了在短时间内我应该也解不出来,所以附上一个AC自动机解释,下面的代码是关于trie树的
http://blog.csdn.net/niushuai666/article/details/7002823

//#include<cstdio>
//#include<string>
#include<iostream>
//#include<algorithm>
#include<memory.h>
//#include<map>
#define MAXNUM 1005
using namespace std;
struct put
{
    int x;
    int y;
    char C;
};
int clockwise[8][2] = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 } };
char puzzles[MAXNUM][MAXNUM], words[MAXNUM];
int L, C, W;
put output[MAXNUM];
class node
{
public:
    int index;
    node* next[26];
    node()
    {
        index = -1;
        for (int i = 0; i < 26; i++)
            next[i] = NULL;
    }
};
class Trie
{
public:
    node *root;
    Trie()
    {
        root = new node();
    }
    void find(int i, int j, int dir)
    {
        node* curr = root->next[puzzles[i][j] - 'A'];
        int x = i, y = j;
        while (curr != NULL)
        {
            if (curr->index > 0)
            {
                output[curr->index].x = x;
                output[curr->index].y = y;
                output[curr->index].C = dir + 'A';
            }
            i += clockwise[dir][0];
            j += clockwise[dir][1];
            if (i<1 || i>L || j < 1 || j>C)
                break;
            curr = curr->next[puzzles[i][j] - 'A'];
        }
    }
    void insert(char *s, int id)
    {
        int i, j, len;
        node *curr = root;
        len = strlen(s);
        for (i = 0; i < len; i++)
        {
            j = s[i] - 'A';
            if (curr->next[j] == NULL)
                curr->next[j] = new node();
            curr = curr->next[j];
        }
        curr->index = id;
    }
};
int main()
{
    freopen("1.txt", "r", stdin);
    int i, j, k;
    Trie T;
    scanf("%d%d%d", &L, &C, &W);
    for (i = 1; i <= L; i++)
        scanf("%s", puzzles[i] + 1);
    for (i = 1; i <= W; i++)
    {
        scanf("%s", words);
        T.insert(words, i);
    }
    for (i = 1; i <= L; i++)
    for (j = 1; j <= C; j++)
    for (k = 0; k < 8; k++)
    {
        T.find(i, j, k);
    }
    for (i = 1; i <= W; i++)
        printf("%d %d %c\n", output[i].x - 1, output[i].y - 1, output[i].C);
}

真的要注意字符串的输入问题,这个题调试这么久就是因为输入字符串的时候没有完全同意是从0开始还是1开始的!
14304K 1344MS

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值