ac自动机加优化
很显然这题就是建好ac自动机,然后我在建立的时候把count的值(每个字符串结尾计数用的),通过fail指针放在一起,那么如果最长的包含了其他所有的串,那么最后所有的count值都会汇聚在最长字符串的那条链上,扫一遍就可以了。
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAX=26;
typedef struct Trie_Node {
struct Trie_Node *fail;
struct Trie_Node *next[MAX];
int count;
}Trie;
int tot;
Trie* Q[200010];
Trie Tree[200010];
char keyword[100010], str[100010];
int head, tail, len, n;
void insert(Trie *root,char *word) {
Trie *p = root;
int i = 0;
while (word[i] != '\0') {
if(p->next[word[i]-'a'] == NULL) {
Trie *temp = &Tree[tot++];
for(int j = 0; j < MAX; j++) temp->next[j] = NULL;
temp->count = 0;
temp->fail = NULL;
p->next[word[i]-'a'] = temp;
}
p = p->next[word[i]-'a'];
i++;
}
p->count++;
}
void build_ac(Trie *root) {
root->fail = NULL;
head = tail = 0;
Q[head++] = root;
while(head != tail) {
Trie *temp = Q[tail++];
Trie *p = NULL;
for(int i = 0; i < MAX; i++) {
if(temp->next[i] != NULL) {
if (temp == root) temp->next[i]->fail = root;
else {
p = temp->fail;
while(p != NULL) {
if(p->next[i] != NULL) {
temp->next[i]->fail = p->next[i];
temp->next[i]->count += p->next[i]->count;
p->next[i]->count = 0;
break;
}
p = p->fail;
}
if (p == NULL) temp->next[i]->fail = root;
}
Q[head++] = temp->next[i];
}
}
}
}
int query(Trie *root) {
int i = 0;
int cnt = 0;
int len = strlen(str);
Trie *p = root;
int index;
while (i < len) {
index = str[i]-'a';
//while (p->next[index] == NULL && p != root) p = p->fail;
p = p->next[index];
//if (p == NULL) p = root;
Trie *temp = p;
/*while (temp != root) {
if (temp->count < 0) break;
cnt += temp->count;
temp->count = -1;
temp = temp->fail;
}*/
cnt += p->count;
i++;
}
return cnt;
}
void del(Trie *root) {
for (int i = 0; i < MAX; i++)
if (root->next[i] != NULL)
del(root->next[i]);
free(root);
}
int main() {
freopen("input.txt","r",stdin);
int T;
scanf("%d", &T);
while(T--) {
len = 0;
tot = 0;
scanf("%d", &n);
Trie *root = &(Tree[tot++]);
root->count = 0;
root->fail = NULL;
for(int i = 0; i < MAX; i++) root->next[i] = NULL;
int end = 0, cnt;
for(int i = 0; i < n; i++) {
scanf("%s", &keyword);
insert(root, keyword);
if (strlen(keyword) > len) {
//str = keyword;
len = strlen(keyword);
for (int i = 0; i <= len; i++) str[i] = keyword[i];
}
}
build_ac(root);
if (query(root) == n) printf("%s\n", str);
else printf("No\n");
//del(root);
}
}
最后是当时犯了很蠢的错误,还加了fread的版本
QAQ
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAX=26;
const int BUFSIZE = 9<<20;
char Buf[BUFSIZE+1], *buf = Buf, *st = Buf;
void scan(int &a) {
for (a = 0; (*buf < '0' || *buf > '9') && st + BUFSIZE != buf; buf++);
while (*buf >= '0' && *buf <= '9' && st + BUFSIZE != buf) {a=a*10+(*buf-'0'); buf++;}
}
int scanString(char *c) {
for (;(*buf == '\n' || *buf =='\t' || *buf == ' ') && st+BUFSIZE != buf; buf++);
int cnt = 0;
while((*buf != '\n') && (*buf != '\t') && (*buf != ' ') && st+BUFSIZE != buf) {*(c++) = *(buf++); cnt++;}
*c = 0;
return cnt;
}
typedef struct Trie_Node {
struct Trie_Node *fail;
struct Trie_Node *next[MAX];
int count;
}Trie;
int tot;
Trie* Q[200010];
Trie Tree[200010];
char keyword[100010], str[100010];
int head, tail, len, n;
void insert(Trie *root,char *word) {
Trie *p = root;
int i = 0;
while (word[i] != '\0') {
if(p->next[word[i]-'a'] == NULL) {
Trie *temp = &Tree[tot++];
for(int j = 0; j < MAX; j++) temp->next[j] = NULL;
temp->count = 0;
temp->fail = NULL;
p->next[word[i]-'a'] = temp;
}
p = p->next[word[i]-'a'];
i++;
}
p->count++;
}
void build_ac(Trie *root) {
root->fail = NULL;
head = tail = 0;
Q[head++] = root;
while(head != tail) {
Trie *temp = Q[tail++];
Trie *p = NULL;
for(int i = 0; i < MAX; i++) {
if(temp->next[i] != NULL) {
if (temp == root) temp->next[i]->fail = root;
else {
p = temp->fail;
while(p != NULL) {
if(p->next[i] != NULL) {
temp->next[i]->fail = p->next[i];
temp->next[i]->count += p->next[i]->count;
p->next[i]->count = 0;
break;
}
p = p->fail;
}
if (p == NULL) temp->next[i]->fail = root;
}
Q[head++] = temp->next[i];
}
}
}
}
int query(Trie *root) {
int i = 0;
int cnt = 0;
int len = strlen(str);
Trie *p = root;
int index;
while (i < len) {
index = str[i]-'a';
//while (p->next[index] == NULL && p != root) p = p->fail;
p = p->next[index];
//if (p == NULL) p = root;
Trie *temp = p;
/*while (temp != root) {
if (temp->count < 0) break;
cnt += temp->count;
temp->count = -1;
temp = temp->fail;
}*/
cnt += p->count;
i++;
}
return cnt;
}
void del(Trie *root) {
for (int i = 0; i < MAX; i++)
if (root->next[i] != NULL)
del(root->next[i]);
free(root);
}
int main() {
freopen("input.txt","r",stdin);
int T;
fread(Buf, 1, BUFSIZE, stdin);
scan(T);
while(T--) {
len = 0;
tot = 0;
scan(n);
Trie *root = &(Tree[tot++]);
root->count = 0;
root->fail = NULL;
for(int i = 0; i < MAX; i++) root->next[i] = NULL;
int end = 0, cnt;
for(int i = 0; i < n; i++) {
cnt = scanString(keyword+end);
if (st+BUFSIZE == buf) {
i--;
end += cnt;
fread(Buf, 1, BUFSIZE, stdin);
buf = Buf;
continue;
}
end = 0;
insert(root, keyword);
if (strlen(keyword) > len) {
//str = keyword;
len = strlen(keyword);
for (int i = 0; i <= len; i++) str[i] = keyword[i];
}
}
build_ac(root);
if (query(root) == n) printf("%s\n", str);
else printf("No\n");
//del(root);
}
}