题目链接:点击打开链接
题意:
给定两人对话的内容;
问给的字符串是否符合两个人的对话;
理解:
很久以前做过这个题;
但是是瞎暴力做的,各种姿势超时;
现在会了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;
}