【AC自动机】【HDOJ2222】 Keywords Search

本文提供了一道经典的AC自动机题目HDOJ2222实战练习,通过具体代码示例详细介绍了如何使用AC自动机解决字符串匹配问题,并给出了两种实现方式:普通指针版和前向星版。
HDOJ 2222 Keywords Search

刚学完AC自动机拿来练手的题 纯模板题

附一个讲解自动机的不错的博客:http://blog.csdn.net/niushuai666/article/details/7002823

结合本题可加深理解

普通指针版

#include
  
   
#include
   
    
#include
    
     
using namespace std;
typedef struct node
{
    int count;
    node* fail;
    node* next[26];
    node()
    {
        count=0;
        fail=NULL;
        for(int i=0;i<26;++i)
            next[i]=NULL;
    }
}*Pnode;
Pnode insert(Pnode root,string s)
{
    Pnode p=root;
    int len=s.size();
    for(int i=0;i
     
      next[s[i]-'a'])
            p->next[s[i]-'a']=new node();
        p=p->next[s[i]-'a'];
    }
    p->count++;
    return root;
}
void build_AC(Pnode root)
{
    Pnode p,temp;
    queue
      
        q;
    q.push(root);
    while(!q.empty())
    {
        p=q.front();
        q.pop();
        for(int i=0;i<26;++i)
            if(p->next[i])
            {
                if(p==root)
                    p->next[i]->fail=root;
                else
                {
                    temp=p->fail;
                    while(temp)
                    {
                        if(temp->next[i])
                        {
                            p->next[i]->fail=temp->next[i];
                            break;
                        }
                        temp=temp->fail;
                    }
                    if(!temp)
                        p->next[i]->fail=root;
                }
                q.push(p->next[i]);
            }
    }
}
int match(Pnode root,string s)
{
    int result=0,len=s.size();
    Pnode p=root,temp;
    for(int i=0;i
       
        next[s[i]-'a']&&p!=root) p=p->fail; p=p->next[s[i]-'a']; if(!p) p=root; temp=p; while(temp!=root&&temp->count!=-1) { result+=temp->count; temp->count=-1; temp=temp->fail; } } return result; } int main() { int t,n; string keyword,s; Pnode root; cin>>t; while(t--) { root=new node(); cin>>n; while(n--) { cin>>keyword; root=insert(root,keyword); } build_AC(root); cin>>s; cout<
        
         <
         
        
       
      
     
    
   
  


前向星版

#include 
  
   
#include 
   
    
#include 
    
     
#include 
     
      
#include 
      
       

using namespace std;

typedef struct Node
{
    int ch[26],fail,cnt;
}Node;

Node nd[555555];
int tp;

void CreateNode(int t)
{
    nd[t].fail  = -1;
    nd[t].cnt = 0;
    for(int i = 0; i < 26; ++i)
        nd[t].ch[i] = -1;
}

void Insert(int root,char *str)
{
    int i,k;
    for(i = 0; str[i]; ++i)
    {
        k = nd[root].ch[str[i]-'a'];
        if(k == -1)
        {
            nd[root].ch[str[i]-'a'] = tp;
            CreateNode(tp);
            root = tp++;
        }else root = k;
    }
    nd[root].cnt++;
}

void Build_AC(int root)
{
    queue 
       
         q; q.push(root); int i,temp; while(!q.empty()) { root = q.front(); q.pop(); for(i = 0; i < 26; ++i) { if(nd[root].ch[i] != -1) { if(!root) nd[nd[root].ch[i]].fail = 0; else { temp = nd[root].fail; while(temp && nd[temp].ch[i] == -1) temp = nd[temp].fail; nd[nd[root].ch[i]].fail = (nd[temp].ch[i] == -1)? temp: nd[temp].ch[i]; } q.push(nd[root].ch[i]); } } } } int Search(int root,char *str) { int cnt = 0,temp,i; for(i = 0; str[i]; ++i) { while(root && nd[root].ch[str[i]-'a'] == -1) root = nd[root].fail; root = nd[root].ch[str[i]-'a']; if(root == -1) root = 0; temp = root; while(temp && nd[temp].cnt != -1) { cnt += nd[temp].cnt; nd[temp].cnt = -1; temp = nd[temp].fail; } } return cnt; } int main() { int t,n; char str[1111111]; scanf("%d",&t); while(t--) { tp = 1; CreateNode(0); scanf("%d",&n); while(n--) { scanf("%s",str); Insert(0,str); } Build_AC(0); scanf("%s",str); printf("%d\n",Search(0,str)); } return 0; } 
       
      
     
    
   
  


内容概要:本研究聚焦于绿电直连型电氢氨园区的优化运行,提出一种集成绿色电力直接供给、电解水制氢及氢气合成氨工艺的综合能源系统架构。通过建立包含风光发电、电解槽、氨合成反应器、储氢罐、电网交互及多类型负荷在内的系统模型,综合考虑绿电直供优先、能量梯级利用与多能互补原则,构建以系统综合运行成本最小化为目标的优化调度模型。研究采用Matlab与Python工具进行算法求解和仿真分析,利用实际气象与负荷数据完成案例验证,评估了不同运行策略下系统的经济性、可再生能源消纳能力与碳减排效益,为新型电氢氨一体化园区的规划与运行提供了理论依据和技术支撑。; 适合人群:具备一定电力系统、新能源或化工背景的研究生、科研人员及从事综合能源系统规划与优化工作的工程技术人员。; 使用场景及目标:①用于科研学习,理解电-氢-氨多能转换系统的建模与优化方法;②为工业园区的低碳化、智能化改造提供技术参考与决策支持;③作为开发类似综合能源管理系统的理论基础。; 阅读建议:此资源包含完整的模型代码、数据与论文,使用者应结合代码仔细研读论文中的模型构建部分,重点关注目标函数与约束条件的设计逻辑,并尝试修改参数进行仿真,以深入掌握优化算法在实际系统中的应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值