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

哪位大牛帮看看,怎么老是WA啊。hash+二分查找

Posted by gxlzlihao at 2010-04-10 16:09:39 on Problem 1035
//poj1035 --spell checker
#include<iostream>
#define MAX 393
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
struct words
{
	char word[20];   //字典单个字符串
	int count;       //在字典中第几个出现
};
int maxnumber[MAX];
int number;
words lihao[MAX][10000];
bool cmp(words& x,words& y)
{
	return x.count <y.count ;
}
int compare(const void* x,const void* y)
{
	if( strcmp(((words*)x)->word,((words*)y)->word)>0 )
		return 1;
	else if(strcmp(((words*)x)->word,((words*)y)->word)<0)
		return -1;
	else 
		return 0;
}
//判断两个字符串是否只有一个字母不同
bool check(char* x,char* y)
{
	int num1=0,num2=0;
	while(x[num1]!='\0')
		++num1;
	while(y[num2]!='\0')
		++num2;
	if(num1==num2)
	{
		int countoferror=0;
		for(int i=0;i!=num1;++i)
			if(x[i]!=y[i])
				++countoferror;
		if(countoferror==1)
			return true;
		else
			return false;
	}
	else if(num1-num2==1)
	{
		int i=0,j=0;
		bool check1=true,check2=true;
		for(;i!=num1 && j!=num2;)
			if(x[i]!=y[j])
			{
				if(check1==false)
				{
					check2=false;
					break;
				}
				else
				{
					++i;
					check1=false;
				}
			}
			else
				++i,++j;
		return check2;
	}
	else if(num1-num2==-1)
	{
		int i=0,j=0;
		bool check1=true,check2=true;
		for(;i!=num1 && j!=num2;)
			if(x[i]!=y[j])
			{
				if(check1==false)
				{
					check2=false;
					break;
				}
				else
				{
					++j;
					check1=false;
				}
			}
			else
				++i,++j;
		return check2;
	}
	else
		return false;
}
int main()
{	
	memset(maxnumber,0,sizeof(maxnumber));
	char temp[16];
	number=0;
	int k;
	while(cin>>temp,temp[0]!='#')
	{
		words tt;
		strcpy(tt.word ,temp );
		tt.count =number++;
		int num=0;
		k=0;
		//hash既字符串各项字母和a的差之和
		while(temp[k]!='\0')
			num+=(temp[k]-'a'),++k;
		lihao[num][maxnumber[num]++]=tt;
	}
	int i,j;
	for(i=0;i!=MAX;++i)
		sort(lihao[i],lihao[i]+maxnumber[i],cmp);
	while(cin>>temp,temp[0]!='#')
	{
		int num=0;
		k=0;
		//hash方法和上面的相同
		while(temp[k]!='\0')
			num+=(temp[k]-'a'),++k;
		words* zhaomin=new words;
		strcpy(zhaomin->word,temp); 
		//二分查找
		words* iter=(words*)bsearch(zhaomin,lihao[num],maxnumber[num],sizeof(words),compare);
		if(iter!=NULL)
			cout<<temp<<" is correct"<<endl;
		else
		{
			vector<words> paixu;
			for(j=num-25>=0?num-25:0;j!=num+26 && j!=MAX;++j)
				for(i=0;i!=maxnumber[j];++i)
					if(check(temp,lihao[j][i].word))
						paixu.push_back(lihao[j][i]);
			sort(paixu.begin(),paixu.end(),cmp);
			cout<<temp<<":";
			for(i=0;i!=paixu.size();++i)
			if(strcmp(paixu[i].word,temp)!=0)
				cout<<" "<<paixu[i].word;
			cout<<endl;
		}
		delete zhaomin;
	}
	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