UVa10887 - Concatenation of Languages

#include <stdio.h>
#include <string.h>

#define MAX 2300000
#define N 30
#define HASHSIZE 1000003

char str[MAX][N];
int head[HASHSIZE], next[MAX];

int BKDRHash(char *str);
int insert(int s);

int main()
{
	int t;
	int n, m;
	int i, j, k, l;
	char temp[N];
	int ans;

#ifndef ONLINE_JUDGE
	freopen("d:\\UVa\\uva_in.txt", "r", stdin);
#endif

	gets(temp);
	sscanf(temp, "%d", &t);
	for (i = 0; i < t; i++)
	{
		/*这当中gets与scanf交换使用时,应注意,当scanf用来输入字符串时,再用gets时会将换行符读入*/
		gets(temp);
		sscanf(temp, "%d%d", &m, &n);

		ans = 0;
		memset(head, -1, sizeof(head));
		for (l = 0; l < m; l++)
			gets(str[l]);
		for (j = 0; j < n; j++)
		{
			gets(temp);
			for (k = 0; k < m; k++)
			{
				strcpy(str[l + k], str[k]);
				strcpy(str[l + k] + strlen(str[k]), temp);
				if (insert(l + k))
					ans++;
			}
			l = l + m;
		}

		printf("Case %d: %d\n", i + 1, ans);
	}
	return 0;
}

int BKDRHash(char *str)
{
	int seed = 31, v = 0;

	while (*str)
	{
		v = v * seed + *(str++);
	}

	return (v & 0X7FFFFFFF) % HASHSIZE;
}

int insert(int s)
{
	int h = BKDRHash(str[s]);
	int u = head[h];

	while (u != -1)
	{
		if (strcmp(str[u], str[s]) == 0)
			return 0;
		u = next[u];
	}

	next[s] = head[h];
	head[h] = s;

	return 1;
}


你可能感兴趣的:(insert,include,concatenation)