Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

本人用Karp-Rabin 和Hash, 各位看看为什么runtime error

Posted by ss08zhangchi at 2010-01-23 16:30:24 on Problem 1200 and last updated at 2010-01-23 16:37:31
#include <stdio.h>
#include <string.h>
#include <math.h>

int main ()
{
	int		N, NC, count, value, h, d, i, len ;
	int		table[512], hashtable[16000000] ;
	char	text[1000000] ;

	while (scanf ("%d%d%s", &N, &NC, text) != EOF)
	{
		memset (table, 0xff, sizeof (table)) ;
		memset (hashtable, 0, sizeof (hashtable)) ;

		h = pow ((double) NC, (double) (N - 1)) ;
		d = NC ; //进制数
		count = 0 ;
		value = 0 ;//用来保存每个定长子串对应的整型值
		len = strlen (text) ;

		for (i = 0 ; i < len ; i++)  //将字符串中每个会出现的字符映射到一个整型值,这个整型值的范围是[0, NC - 1]
			if (table[text[i]] == -1)
				table[text[i]] = count++ ;

		for (i = 0 ; i < N ; i++)  //每个定长字串对应一个整型值,而且这个值的范围是[0, NC^N - 1]

			value = d * value + table[text[i]] ;

		hashtable[value] = 1 ;
		count = 1 ;

		for (i = 1 ; i + N - 1 < len ; i++)
		{
			value = d * (value - h * table[text[i - 1]]) + table[text[i + N - 1]] ;
			if (hashtable[value] == 0)
			{
				hashtable[value] = 1 ;
				count++ ;
			}
		}

		printf ("%d\n", count) ;
	}
	return 0 ;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator