Hdu-2222 Keywords Search(AC自动机)

本文介绍了一种使用AC自动机实现的图像检索系统,该系统能够通过匹配关键字来搜索图片描述,实现高效的图像搜索功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey(威士忌酒) also wants to bring this feature(特写) to his image retrieval(检索) system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify(简化) the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
 

Input
First line will contain one integer(整数) means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
 

Output
Print how many keywords are contained in the description.
 

Sample Input
1 5 she he say shr her yasherhs
 

Sample Output
3
 

Author
Wiskey
 

Recommend
lcy
 


分析:AC自动机模板题。


#include <queue>
#include <cstdio>
#include <cstring> 
#include <iostream>
#define MAXNODE 500005
using namespace std;
int n,T;
char s[1000005];
struct ACautomata
{
	int ch[MAXNODE][26];
	int f[MAXNODE];             // fail函数
	int val[MAXNODE]; 	        // 是否为单词结尾 
	int last[MAXNODE];          // 后缀链接 
	int cnt[10005];             // 每个单词出现次数 
	int tot;                    // trie 单词总数 
	int num;                    // 单词出现了几个 
	int time[10005];
	void init()
	{
		num = 0;
		tot = 1;
		memset(ch[0],0,sizeof(ch[0]));
		memset(cnt,0,sizeof(cnt));
		memset(time,0,sizeof(time));
	}
	int idx(char c)
	{
		return c - 'a';
	}
	void insert(char *s,int v)
	{
		int u = 0,n = strlen(s);
		for(int i = 0;i < n;i++)
		{
			int c = idx(s[i]);
			if(!ch[u][c])
			{
				memset(ch[tot],0,sizeof(ch[tot]));
				val[tot] = 0;
				ch[u][c] = tot++; 
			}
			u = ch[u][c];
		}
		if(val[u]) time[val[u]]++;
		else val[u] = v,time[v] = 1;
	}
	void print(int i,int j)
	{
		if(j) 
		{
			if(!cnt[val[j]]) num += time[val[j]];
			cnt[val[j]]++;
			print(i,last[j]);
		}
	}
	void find(char *T)
	{
		int n = strlen(T);
		int j = 0;
		for(int i = 0;i < n;i++)
		{
			int c = idx(T[i]);
			j = ch[j][c];
			if(val[j]) print(i,j);
			else if(last[j]) print(i,last[j]);
		}
	}
	void getFail()
	{
		queue<int> q;
		f[0] = 0;
		for(int c = 0;c < 26;c++)
		{
			int u = ch[0][c];
			if(u)
			{
				f[u] = 0;
				q.push(u);
				last[u] = 0;
			}
		}
		while(!q.empty())
		{
			int r = q.front();q.pop();
			for(int c = 0;c < 26;c++)
			{
				int u = ch[r][c];
				if(!u)
				{
					ch[r][c] = ch[f[r]][c];
					continue;
				}
				q.push(u);
				int v = f[r];
				f[u] = ch[v][c];
				last[u] = val[f[u]] ? f[u] : last[f[u]];
			}
		}
	}
} tree;
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		tree.init();
		scanf("%d",&n);
		for(int i = 1;i <= n;i++)
		{
			scanf("%s",s);
			tree.insert(s,i);
		}
		tree.getFail();
		scanf("%s",s);
		tree.find(s);
		cout<<tree.num<<endl;
	}
} 


内容概要:本文档详细介绍了基于弹性架构搜索(Elastic Architecture Search, EAS)结合Transformer编码器进行多变量时间序列预测的项目实例。项目旨在自动化优化多变量时间序列预测模型结构,提升预测精度与鲁棒性,降低计算资源消耗,实现模型轻量化。通过MATLAB实现,项目采用Transformer编码器的多头自注意力机制,结合EAS的弹性权重共享和分阶段搜索策略,解决了高维多变量时间序列的复杂依赖建模、架构搜索计算资源需求高、模型过拟合、多步预测误差积累、数据异构性与缺失值处理、复杂模型训练收敛等挑战。最终,项目构建了一个高度模块化和可扩展的系统设计,适用于智能制造、能源管理、智慧交通等多个工业场景。 适合人群:具备一定编程基础,对时间序列预测、深度学习及MATLAB有一定了解的研发人员和研究人员。 使用场景及目标:①自动化优化多变量时间序列预测模型结构,提升预测精度与鲁棒性;②降低计算资源消耗,实现模型轻量化;③实现高度模块化与可扩展的系统设计,促进人工智能在工业领域的深度应用;④提供科研与教学的典范案例与工具,探索深度学习架构搜索在时序预测的前沿技术;⑤促进多变量时序数据融合与异质信息处理能力,推动MATLAB深度学习工具箱的应用与扩展。 其他说明:项目不仅聚焦于模型性能提升,更注重计算资源节约和应用落地的可行性。借助弹性架构搜索自动化调参,减少人工经验依赖,加快模型迭代速度,降低开发门槛。结合Transformer编码器的表达能力,显著改善多变量时间序列预测中的长期依赖捕捉和异质数据融合问题,为各类时间序列分析任务提供一种全新的解决方案。项目通过详细的代码实现和注释,帮助用户理解Transformer机制与弹性架构搜索如何协同工作,实现多变量时间序列预测。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值