用拉链hash做的,全WA,想知道原因,求大佬指点,拜托拜托QAQ
查看原帖
用拉链hash做的,全WA,想知道原因,求大佬指点,拜托拜托QAQ
501987
Start_sky楼主2021/9/13 16:43

Code:


#include<bits/stdc++.h>

#define mod 10007

using namespace std;

vector<int> hashs[10007];
int n,m,pos;

int hash1(char chs[]){				//将字符串hash 
	int ans = 0;
	for(int i = 0;i<strlen(chs);i++){
		ans = (ans*26+chs[i]+ mod) % mod;
	}
	return ans;
}

bool check(int i,int pos){			//去重 
	vector<int>::iterator it;
	it = find(hashs[pos].begin(),hashs[pos].end(),i);
	if(it!=hashs[pos].end())
		return false;
	else
		return true;
}


int main()
{	
	cin>>n;getchar();
	
	for(int i = 1;i<=n;i++){
		int count;
		cin>>count;
		for(int j = 0;j<count;j++){
			char chs[25];
			scanf("%s",chs);
			pos = hash1(chs);
			if(check(i,pos))
				hashs[pos].push_back(i);
		}
	}
	cin>>m;getchar();
	
	for(int i = 1;i<=m;i++){
		char item[25];
		scanf("%s",item);
		int poss = hash1(item);
		if(!hashs[poss].empty()){		//若存在,则输出
			for(int j = 0;j<hashs[poss].size();j++){
				cout<<hashs[poss][j];
				if(j!=hashs[poss].size()-1)
				cout<<" ";
			}
			cout<<endl;
		}
		else
			cout<<endl;
	}
	return 0;
}
2021/9/13 16:43
加载中...