Keywords Search

题目描述

原题来自:HDU 2222

给定 nn 个长度不超过 5050 的由小写英文字母组成的单词准备查询,以及一篇长为 mm 的文章,问:文中出现了多少个待查询的单词。多组数据。

输入格式

第一行一个整数 TT,表示数据组数;
对于每组数据,第一行一个整数 nn,接下去 nn 行表示 nn 个单词,最后一行输入一个字符串,表示文章。

输出格式

对于每组数据,输出一个数,表示文中出现了多少个待查询的单词。

样例

InputcopyOutputcopy
1
5
she
he
say
shr
her
yasherhs
3

数据范围与提示

对于全部数据,1\le n\le 10^4,1\le m\le 10^61≤n≤104,1≤m≤106。


#include<iostream>
#include<vector>
#include <cstring>
#include<algorithm>
using namespace std;
void get_next(char ch[],int l,int next[])
{
	next[0]=-1;
	int i=0,j=-1;
	while(i<=l)
	{
		if(j==-1||ch[i]==ch[j])
		next[++i]=++j;
		else
		j=next[j];
		
	}
}

int num(int ll,int l,int next[],char a[],char b[])
{
	
	int j=0;
	for(int i=0;i<ll;i++)
	{
		while(b[i]!=a[j]&&j>0)
		{
			j=next[j];
		}
		if(b[i]==a[j])
		{
			j++;
		}
		if(j==l)
		{
			return 1;
			j=next[j];
		}
	}
	return 0;
}
struct st
{
	char a[55];
} st[10004];

int main()
{
	int n,m;
	cin>>n;
	char b[1000005];
	int l,ll,next[10005];
	while(n--)
	{
		cin>>m;
		for(int i=0;i<m;i++)
		{
			cin>>st[i].a;
		}
		cin>>b;
		int ans=0;
		ll=strlen(b);
		for(int i=0;i<m;i++)	
		{
		
		l=strlen(st[i].a);
		get_next(st[i].a,l,next);
		ans+=num(ll,l,next,st[i].a,b);
		}
		cout<<ans<<endl;
	}
 } 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linalw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值