Timus 1102 AC自动机

题目链接:点击打开链接


题意:

给定两人对话的内容;

问给的字符串是否符合两个人的对话;


理解:

很久以前做过这个题;

但是是瞎暴力做的,各种姿势超时;

现在会了AC自动机,再把它做了;

其实还比较简单;

但有个问题在于 putone 是错的;

而 inputone 和 outputone 都是对的;

这个就是bug点;

但是找出规律特判下就行了;


自己没看源码敲的AC自动机;

感觉还是不错的;


代码如下


#include <cstdio>
#include <cstring>
#include <cmath>

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>

using namespace std;

typedef long long LL;

const int MAXN = 1e7 + 10;
const int MOD = 1e9 + 7;
const int INF = 0x7fffffff;
const double EPS = 1e-7;

typedef pair<int, int> PII;

#define X first
#define Y second

string pro[] = {"one", "puton", "out", "output", "in", "input"};

struct node {
    node *next[26];
    int cnt;
    node *fail;
    node() {
        for (int i = 0; i < 26; ++i) {
            next[i] = NULL;
            cnt = 0;
            fail = NULL;
        }
    }
};

node *root;

void build_tree(string str) {
    node *p = root;
    for (int i = 0; i < str.length(); ++i) {
        int id = str[i] - 'a';
        if (p->next[id] == NULL) {
            node *q = new node;
            p->next[id] = q;
        }
        p = p->next[id];
    }
    p->cnt = 1;
}

void build_ac() {
    queue<node*> que;
    que.push(root);

    while (!que.empty()) {
        node *p = que.front();
        que.pop();
        for (int i = 0; i < 26; ++i) {
            if (p->next[i] != NULL) {
                que.push(p->next[i]);
                if (p == root) {
                    p->next[i]->fail = root;
                    continue;
                }
                node *temp = p->fail;
                while (temp != NULL && temp->next[i] == NULL) {
                    temp = temp->fail;
                }
                if (temp == NULL) {
                    p->next[i]->fail = root;
                }
                else {
                    p->next[i]->fail = temp->next[i];
                }
            }
        }
    }
}

void init() {
    for (int i = 0; i < 6; ++i) {
        build_tree(pro[i]);
    }
    build_ac();
}

char s[MAXN];

bool solve() {
    int len = strlen(s);
    node *p = root;
    int sum = 0;
    for (int j = 0; j < len; ++j) {
        if (p == root) {
            sum = 0;
        }
        int i = s[j] - 'a';
        if (p->next[i] == NULL) {
            node *temp = p->fail;
            while (temp != NULL && temp->next[i] == NULL) {
                temp = temp->fail;
            }
            if (temp == NULL) {
                return false;
            }
            else if (temp == root){
                if (p->cnt == 0) {
                    return false;
                }
            }
            p = temp->next[i];
            if (temp == root) {
                sum = 0;
            }
        }
        else {
            p = p->next[i];
        }
        sum += p->cnt;
        if (s[j] == 'e') {
            if (sum == 2) {
                return false;
            }
        }
    }
    if (p->cnt != 0) {
        return true;
    }
    else {
        return false;
    }
}

int main() {
    root = new node;
    init();
    int t;
    cin >> t;
    while (t--) {
        scanf("%s", s);
        if (solve()) {
            printf("YES\n");
        }
        else {
            printf("NO\n");
        }
    }

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值