HDU 3065 病毒侵袭持续中 AC自动机
2016-01-26 19:55
429 查看
思路:直接查找即可。
注意:如果不是’A’-‘Z’之间,直接赋值为26即可,因为反正也不会找到它的。
http://acm.hdu.edu.cn/showproblem.php?pid=3065/********************************************* Problem : HDU 2896 Author : NMfloat InkTime (c) NM . All Rights Reserved . ********************************************/ #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 ++) //遍历一个STL容器 #define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next) //遍历u所连接的点 #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 = 50005; const int MAX_SIZE = 27; int tot ; char s[2000005]; char sc[1005][55]; int virus[4];//需要输出病毒的编号 int recv[4]; int ans[1005]; class AC_automation { private: struct Node { int next[MAX_SIZE]; //节点的儿子 int stat ; //有几个字符是以当前节点结尾的 }t[MAXN]; //Trie 树上的节点 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] -= 'A'; 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; //printf("%d -> ",nxt); } //puts(""); t[nxt].stat = idx; //给每个字符串编号 rep(i,0,len) str[i] += 'A'; } 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]]) {//处理fail[tmp] = 0 时的情况 if(t[0].next[i]) fail[t[u].next[i]] = t[0].next[i]; } } } q.pop(); } //rep(i,1,tot-1) printf("%d %d %d\n",i,fail[i],t[i].stat); } void query(char * str) { int idx = 0; int len = strlen(str) - 1; rep(i,0,len) { if(str[i] >= 'A' && str[i] <= 'Z') str[i] -= 'A'; else str[i] = 26; } 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) { ans[t[tmp].stat] ++; } tmp = fail[tmp]; } } } }; AC_automation ac; void input() { ac.init(); getchar();//读掉一个回车 rep(i,1,n) { gets(sc[i]); ac.insert(sc[i],i); } ac.get_fail(); } void solve() { scanf("%s",s); cls(ans,0); ac.query(s); rep(i,1,n) { if(ans[i]) { printf("%s: %d\n",sc[i],ans[i]); } } } int main(void) { //freopen("a.in","r",stdin); while(~scanf("%d",&n)) { input(); solve(); } return 0; }
相关文章推荐
- unix环境高级编程之信号篇(二)
- quickcocos2d-x BMFont自定义字体 fnt使用 以及工具地址下载
- 回调方法介绍之中国好室友篇(Java示例)
- 找出微信把你删除的好友
- Runtime中property_getName与property_getAttributes
- 对照地图看历史剧,学习历史地理
- MyBatis使用@MapKey注解接收多个查询记录到Map中,以便方便地用get()方法获取字段的值
- Android layout_weight layout_width ,Listview单行特定textview省略
- java类的生命周期分析
- HBase-7.hbase查询多版本数据&过滤器原则&批量导入Hbase&hbase预分区
- hdu 5583 Kingdom of Black and White
- android EditText多行文本输入的若干问题
- 事件和进程间的数据交换
- 如何使用Navicat备份数据库脚本
- HDU acm step:Chapter one section three(简单贪心)
- MySQL数据库的JDBC连接方法
- Redis持久化方式介绍
- Windows Mobile 常用键值(VK)对应表
- 正则表达式规则
- docker 镜像的保存以及导入