思路:对每个字符串在Trie树上找一遍即可。
注意:理论应该有10w个节点,但是10w个MLE了,所以开8w也能过。
http://acm.hdu.edu.cn/showproblem.php?pid=2896
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define rep(i,a,b) for(int i = (a) ; i <= (b) ; i ++)
#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --)
#define repS(it,p) for(auto it = p.begin() ; it != p.end() ; it ++)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x) memset(a,x,sizeof(a))
#define eps 1e-8
using namespace std;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef unsigned long long ULL;
int T,n,m,k;
int fx[] = {0,1,-1,0,0};
int fy[] = {0,0,0,-1,1};
const int MAXN = 80005;
const int MAX_SIZE = 95;
int tot ;
char s[10005];
int virus[4];
int recv[4];
class AC_automation {
private:
struct Node {
int next[MAX_SIZE];
int stat ;
}t[MAXN];
int fail[MAXN];
public:
void init() {
tot = 1; cls(t,0); cls(fail,0);
}
void reset_stat(int len) {
rep(i,1,len) t[recv[i]].stat = virus[i];
}
void insert(char * str,int idx) {
int len = strlen(str) - 1;
rep(i,0,len) str[i] -= ' ';
int pre = 0 , nxt = 0 ;
rep(i,0,len) {
nxt = t[pre].next[str[i]];
if(nxt == 0) {
t[pre].next[str[i]] = tot ;
nxt = tot ++;
}
pre = nxt;
}
t[nxt].stat = idx;
}
void get_fail() {
queue<int>q;
q.push(0);
while(!q.empty()) {
int u = q.front();
rep(i,0,MAX_SIZE-1) {
if(t[u].next[i]) {
q.push(t[u].next[i]);
int tmp = u;
while(fail[tmp]) {
if(t[fail[tmp]].next[i]) {
fail[t[u].next[i]] = t[fail[tmp]].next[i]; break;
}
tmp = fail[tmp];
}
if(u != 0 && !fail[t[u].next[i]]) {
if(t[0].next[i]) fail[t[u].next[i]] = t[0].next[i];
}
}
}
q.pop();
}
}
int query(char * str) {
int idx = 0;
int len = strlen(str) - 1;
rep(i,0,len) str[i] -= ' ';
int now = 0;
rep(i,0,len) {
while(now && (!t[now].next[str[i]])) now = fail[now];
now = t[now].next[str[i]];
int tmp = now;
while(tmp) {
if(t[tmp].stat) {
idx ++ ;
virus[idx] = t[tmp].stat;
recv[idx] = tmp;
t[tmp].stat = 0;
}
tmp = fail[tmp];
}
}
return idx;
}
};
AC_automation ac;
void input() {
char str[205];
ac.init();
getchar();
rep(i,1,n) {
gets(str);
ac.insert(str,i);
}
ac.get_fail();
}
void solve() {
scanf("%d",&m);
getchar();
int web_site;
int cnt = 0;
rep(i,1,m) {
gets(s);
rep(i,1,3) virus[i] = 0;
int len = ac.query(s);
if(len > 0) {
if(len >= 2) if(virus[1] > virus[2]) {swap(virus[1],virus[2]);swap(recv[1],recv[2]);}
if(len >= 3) if(virus[2] > virus[3]) {swap(virus[3],virus[2]);swap(recv[3],recv[2]);}
if(len >= 3) if(virus[1] > virus[2]) {swap(virus[1],virus[2]);swap(recv[1],recv[2]);}
cnt ++;
printf("web %d:",i);
rep(i,1,len) printf(" %d",virus[i]);
puts("");
}
ac.reset_stat(len);
}
printf("total: %d\n",cnt);
}
int main(void) {
while(~scanf("%d",&n)) {
input();
solve();
}
return 0;
}