标签:style blog class code c tar
挺扯的一个题,解码有点问题+注意用int存,跟HDU2222差不多...
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #include <algorithm> #include <cstdlib> using namespace std; #define N 60010 int trie[N][260]; int que[100000]; int fail[100000]; int hash[1001]; int o[100001]; int str[100000]; int t,num; void CL() { memset(trie,-1,sizeof(trie)); memset(o,0,sizeof(o)); t = 1; } int fun(char x) { if(x >= ‘A‘&&x <= ‘Z‘) return x-‘A‘; else if(x >= ‘a‘&&x <= ‘z‘) return x-‘a‘+26; else if(x >= ‘0‘&&x <= ‘9‘) return x-‘0‘+52; else if(x == ‘+‘) return 62; else return 63; } void judge(char *s) { int i,j,len,temp,n; len = strlen(s); n = 0; for(i = 0; i < len; i ++) { if(s[i] == ‘=‘) { n -= 2;//开始这里没理解对。 continue; } temp = fun(s[i]); for(j = 5; j >= 0; j --) { if(temp&(1<<j)) que[n++] = 1; else que[n++] = 0; } } for(i = 0; i < n;) { str[i/8] = 0; for(j = 7; j >= 0; j --) { if(que[i]) str[i/8] += 1<<j; i ++; } } num = n/8; } void insert(int x) { int i,len,root; len = num; root = 0; for(i = 0; i < len; i ++) { if(trie[root][str[i]] == -1) { trie[root][str[i]] = t ++; } root = trie[root][str[i]]; } o[root] = x; } void build_ac() { int head,tail,front,i; head = tail = 0; for(i = 0; i < 260; i ++) { if(trie[0][i] != -1) { fail[trie[0][i]] = 0; que[tail++] = trie[0][i]; } else { trie[0][i] = 0; } } while(head != tail) { front = que[head++]; for(i = 0; i < 260; i ++) { if(trie[front][i] != -1) { que[tail++] = trie[front][i]; fail[trie[front][i]] = trie[fail[front]][i]; } else { trie[front][i] = trie[fail[front]][i]; } } } } void query() { int len,key,temp,i,root; root = 0; len = num; for(i = 0; i < len; i ++) { temp = str[i]; root = trie[root][temp]; key = root; while(key != 0) { hash[o[key]] = 1; key = fail[key]; } } } int main() { int n,i,m,j; char ch[5000]; while(scanf("%d",&n)!=EOF) { CL(); for(i = 1; i <= n; i ++) { scanf("%s",ch); judge(ch); insert(i); } build_ac(); scanf("%d",&m); for(i = 0; i < m; i ++) { memset(hash,0,sizeof(hash)); scanf("%s",ch); judge(ch); query(); int ans = 0; for(j = 1; j <= n; j ++) { if(hash[j]) ans ++; } printf("%d\n",ans); } printf("\n"); } return 0; }
标签:style blog class code c tar
原文地址:http://www.cnblogs.com/naix-x/p/3728752.html