AC自动机应用 多模式匹配 多个单词在文章中出现的次数-C语言实现
来源:互联网 发布:文件加密解密算法 编辑:程序博客网 时间:2024/05/29 15:53
/*AC自动机应用 多模式匹配 多个单词在文章中出现的次数*/#define N 500char str[1000],keyword[51];int head,tail;// 表示字典树的一个节点 struct node{ node * fail;node * next[26]; int count;node(){fail = NULL;count = 0;for(int i=0;i<26;i++)next[i] = NULL;}} * q[N]; // q,用来逐层访问时的队列结构 node * root;void insert(char * str){int index,i; node * p = root; // p代表当前的节点,开始为根节点int len = strlen(str);for(i=0;i<len;i++){index = str[i] - 'a';if( p->next[index] == NULL)p->next[index] = new node();p = p->next[index]; }p->count++; // 单词的结束节点count不为0,单词组中有多个相同单词,count>1}// 设置失败指针 void build_ac( ){ q[tail++] = root; //入队while(head!=tail){node *p = q[head++];node *temp = NULL;for(int i=0;i<26;i++){ // 处理子节点if(p->next[i]!=NULL){if(p==root){p->next[i]->fail = root;}else{temp = p->fail; // 父节点的fail,沿着fail找,直到找到匹配或者到达根 while(temp!=NULL){ // 中间节点 if(temp->next[i]!=NULL){ // 匹配 p->next[i]->fail = temp->next[i];break;}temp = temp ->fail;}if(temp == NULL){p->next[i]->fail = root;}}q[tail++] = p->next[i];} }} }int query(char str[]){int index,len,result;node * p = root;result = 0;len = strlen(str);for(int i=0;i<len;i++){index = str[i]-'a';while(p->next[index]==NULL&&p!=root) // 所有可能匹配的节点 p = p->fail;p = p->next[index]; // 状态转移 if (p==NULL) // 表示从root转移过来p = root;node * temp = p;// 判断是否是结束单词 while(temp!=root && temp->count!=-1 ){ //&& temp->count!=0result += temp->count;temp->count = -1; // 单词已经出现了,不再计数 temp = temp->fail; // 还有可能匹配的单词,其他的前缀为现在的后缀 } }return result;}int main(){int ncase,num;ncase=1;strcpy(str,"abcdabc");while(ncase--){head = tail = 0;root = new node;char a[]="abc";insert(a);char b[]="bcd";insert(b);char c[]="bc";insert(c);build_ac( );cout<<query(str)<<endl;}return 0;}
0 0
- AC自动机应用 多模式匹配 多个单词在文章中出现的次数-C语言实现
- hdu3065 AC自动机-每个标准串在模式串中出现的次数
- ac自动机实现多模式匹配
- c语言在一段段落中匹配一个单词出现次数
- 多模式匹配算法:AC自动机的C++实现
- hdu5384(AC自动机+纪录重复单词出现的次数)
- c语言统计从文件读取的文章中所有单词的出现次数
- 多模式串匹配之AC自动机算法(Aho-Corasick算法)简介与C语言程序实现源码参考
- 多模式串匹配之AC自动机算法(Aho-Corasick算法)简介与C语言程序实现源码参考
- 统计文章中单词出现的次数
- 统计文章中单词出现的次数
- HDU 3065 病毒侵袭持续中 ac自动机 统计文本串的单词和其出现的次数。
- 多模式匹配--AC自动机算法
- AC自动机(多模式匹配)
- AC自动机-多模式匹配算法
- AC自动机 多模式串匹配 模板
- AC自动机模板(多模式匹配)
- AC自动机模板及对多模式匹配的理解
- 线程与进程的区别与联系
- table滚动条
- 32位和64位ORACLE下操作GOLDENGATE过程记录
- AFNetworking如何post一个json数据到服务器
- 类型安全 与 线程安全 、异常安全、事务安全
- AC自动机应用 多模式匹配 多个单词在文章中出现的次数-C语言实现
- Java-mongodb-AggregationOutput(分组、统计)
- 【c++】模拟实现boost库里的scoped_ptr
- 警察叔叔就是它!那些烦人的空格!
- 解决"Could not initialize class android.graphics.Typeface"
- POJ-2457 SPFA+路径记录
- Unity3d 着色器语法(Shader)
- Android操作系统软键盘的显示与隐藏
- Masonry介绍与使用实践(快速上手Autolayout)