hdoj 3065
来源:互联网 发布:交易开拓者 编程 编辑:程序博客网 时间:2024/05/16 16:14
题目大意:统计一些大写字母不重复的单词在文本中出现的次数。
解题思路:AC自动机。。。水题。
注意重复出现的单词,比如AAA表示单词AA出现了2次
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int kind = 26;const int maxn = 1001;const int maxs = 51;const int maxm = 2000010;struct record{int begin, num;record(){begin = -1;num = 0;}};struct node{node *fail;node *next[kind];bool isLeaf;int index;node(){index = -1;fail = NULL;isLeaf = false;memset(next, 0, sizeof(next));}};char str[maxn * maxs], text[maxm];record re[maxn];int n;int insert(node *root, char *str, int index);void build(node *root);void query(node *root, char *str);int main(){while(scanf("%d", &n) != EOF){int index = 0, len;node *root = new node();char *ss = str;for(int i = 0; i < n; i++){re[i].begin = index;re[i].num = 0;scanf("%s", ss);len = insert(root, ss, i);index += len;ss += len;}build(root);scanf("%s", text);query(root, text);for(int i = 0; i < n; i++){ss = str;if(re[i].num != 0)printf("%s: %d\n", ss + re[i].begin, re[i].num);}}return 0;}int insert(node *root, char *str, int index){node *p = root;int sindex = 0, le = 0;while(str[le] != '\0'){sindex = str[le] - 'A';if(p->next[sindex] == NULL)p->next[sindex] = new node();p = p->next[sindex];le++;}p->isLeaf = true;p->index = index;return le + 1;}void build(node *root){queue<node *> que;node *tmp, *p;que.push(root);while(!que.empty()){tmp = que.front();que.pop();for(int i = 0; i < kind; i++){if(tmp->next[i] != NULL){if(tmp == root)tmp->next[i]->fail = root;else{p = tmp->fail;while(p != NULL){if(p->next[i] != NULL){tmp->next[i]->fail = p->next[i];break;}p = p->fail;}if(p == NULL)tmp->next[i]->fail = root;}que.push(tmp->next[i]);}}}}void query(node *root, char *str){int i = 0, sindex;node *p = root, *tmp;while(str[i] != '\0'){if(str[i] >= 'A' && str[i] <= 'Z'){sindex = str[i] - 'A';while(p != NULL && p->next[sindex] == NULL)p = p->fail;if(p == NULL)p = root;elsep = p->next[sindex];tmp = p;while(tmp != NULL){if(tmp->isLeaf)re[tmp->index].num++;tmp = tmp->fail;}}elsep = root;i++;}}
- hdoj 3065
- hdoj 3065 ac自动机
- HDOJ
- hdoj
- hdoj
- HDOJ
- HDOJ-3065 AC自动机..题意大丈夫?
- HDOJ 3065 病毒侵袭持续中
- HDOJ-3065 病毒侵袭持续中
- HDOJ 3065 病毒侵袭持续中
- hdoj 1568 && hdoj 5344 && hdoj 5444
- 【AC自动机】 HDOJ 3065 病毒侵袭持续中
- HDOJ 2176
- Hdoj--1272
- hdoj 1003
- hdoj 1005
- hdoj 1030
- hdoj 2154
- C++入门进阶之3: 类和对象
- input_ip
- paip.图形化编程厉器.net vs2010 工作流WF4 hello word
- hdoj 2222 Keywords Search
- paip.图形化编程工具VS WF与JBPM的比较
- hdoj 3065
- Silverlight/Windows8/WPF/WP7/HTML5周学习导读(9月3日-9月9日)
- 【每日N题】海量数据处理-1
- Configuration.buildSessionFactory()过时之后,获取SessionFactory的Util类写法及一些设想
- Java大数
- 一个非常好的ABNF免费解析器
- The errors I got before seeing the acutal django cms page...
- c#设置取消ie代理服务器
- android Content Provider详解四