UVA10887--Trie--哈希

本文概述了编程领域的核心技术,包括但不限于前端开发、后端开发、移动开发、游戏开发、大数据开发、开发工具等细分领域。从基础到进阶,涵盖了从语言选择、框架应用到工具使用等多个方面,为开发者提供全面的技术指导。

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

A language is a set of strings. And the concatenation of two languages is the set of all strings that are formed by concatenating the strings of the second language at the end of the strings of the first language.

For example, if we have two language A and B such that:

A = {cat, dog, mouse}

B = {rat, bat}

The concatenation of A and B would be:

C = {catrat, catbat, dograt, dogbat, mouserat, mousebat}

Given two languages your task is only to count the number of strings in the concatenation of the two languages.

Input
There can be multiple test cases. The first line of the input file contains the number of test cases,T (1≤T≤25). ThenT test cases follow. The first line of each test case contains two integers,M andN (M,N<1500), the number of strings in each of the languages. Then the nextMlines contain the strings of the first language. TheN following lines give you the strings of the second language. You can assume that the strings are formed by lower case letters (‘a’ to‘z’) only, that they are less than10 characters long and that each string is presented in one line without any leading or trailing spaces. The strings in the input languages may not be sorted and there will be no duplicate string.

Output

For each of the test cases you need to print one line of output. The output for each test case starts with the serial number of the test case, followed by the number of strings in the concatenation of the second language after the first language.

Sample Input Output for Sample Input

2

3 2

cat

dog

mouse

rat

bat

1 1

abc

cab

Case 1: 6

Case 2: 1

思路:简单的用trie树标记。。关键这题坑啊!!!输入要用gets。。而且初始化也要注意下。。有空串的。。所以val[0] = 0;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
#define maxn 52600000
int ans;
char str1[1600][12],str2[1600][12],str[28];
struct Trie
{
	int first[maxn],nxt[maxn],ch[maxn];
	bool val[maxn];
	int cnt;
	void init()
	{	
		val[0] = 0;
		first[0] = -1;
		cnt = 1;
	}
	
	int idx(char c)
	{
		return c;
	}

	void insert(char * s)
	{
		int len = strlen(s);
		int u = 0;
		for(int i = 0;i < len;i++)
		{
			int c = idx(s[i]);
			bool flag = false;
			int j;
			for(j = first[u];j != -1;j = nxt[j])
			{
				if(ch[j] == c)
				{
					flag = true;
					break;
				}
			}
			if(!flag)
			{
				first[cnt] = -1;
				ch[cnt] = c;
				val[cnt] = 0;
				nxt[cnt] = first[u];
				first[u] = cnt;
				cnt++;
			}
			if(flag)	u = j;
			else u = cnt-1;
		}
		if(!val[u])	ans++;
		val[u] = 1;
	}
}trie;
		
int main()
{
	//freopen("in.txt","r",stdin);
	int t;
	scanf("%d",&t);
	for(int cas = 1;cas <= t;cas++)
	{
		trie.init();
		int n,m;
		scanf("%d%d",&n,&m);
		getchar();
		ans = 0;
		for(int i = 1;i <= n;i++)
			gets(str1[i]);
		for(int i = 1;i <= m;i++)
			gets(str2[i]);
		for(int i = 1;i <= n;i++)
			for(int j = 1;j <= m;j++)
			{
				strcpy(str,str1[i]);
				strcpy(&str[strlen(str1[i])],str2[j]);
				//strcat(str,str2[j]);
				trie.insert(str);
			}
		printf("Case %d: %d\n",cas,ans);
	}
	return 0;
}

用哈希也写了下:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
//1500 1500 20
#define maxn 2260007
int first[maxn],nxt[maxn];
int ans;
char str[maxn][24];
char s[1588][14];
char ss[14];
int Hash(int x)
{
	int len = strlen(str[x]);
	int ans = 0;
	for(int i = 0;i < len;i++)
		ans = ((ans * 17) + str[x][i]) % maxn;
	return ans;
}

void Try_to_insert(int x)
{
	int h = Hash(x);
	for(int i = first[h];i != -1;i = nxt[i])
	{
		if(!strcmp(str[x],str[i]))	return;
	}
	nxt[x] = first[h];
	first[h] = x;
	ans++;
}

void init()
{
	ans = 0;
	memset(first,-1,sizeof(first));
}

int main()
{
	//freopen("in.txt","r",stdin);
	int t;	scanf("%d",&t);
	for(int cas = 1;cas <= t;cas++)
	{
		init();
		int n,m;
		scanf("%d%d",&n,&m);
		getchar();
		for(int i = 1;i <= n;i++)	
			gets(s[i]);
		for(int i = 1;i <= m;i++)
		{
			gets(ss);
			for(int j = 1;j <= n;j++)
			{
				strcpy(str[(i-1)*n+j],s[j]);
				strcpy(&str[(i-1)*n+j][strlen(s[j])],ss);
				Try_to_insert((i-1)*n+j);
			}
		}
		printf("Case %d: %d\n",cas,ans);
	}
	return 0;
}
	


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值