http://acm.dlut.edu.cn/problem.php?id=1247
这道题让我非常痛苦,现学的哈希表,一直T!
后来看了姜神的代码,我们算法上一样的,看起来都几乎一样。
于是我就干脆照着改!改成一模一样!还是T!
在我不得不相信人品的时候,我发现了一个问题。。。只有一句话是和AC代码不一样的。
这句话导致我T,说实话到底为什么T我仍然不是很明白!
见代码内注释!
其他不多说了,给姜神博客连接(想法完全一样,我自己的代码和他只在语言表述上有差异):
http://blog.csdn.net/u013894557/article/details/24270209
AC代码(这个版本是和JJL一模一样的):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int Mod = 2001000;
const int maxm = Mod*10;
/*int abs(int x)
{
return x>0?x:-x;
}
*/
inline int abs(int x)
{
return x&0x7fffffff;
}
struct node
{
int w,next;
}dic[maxm];
int head[Mod],version[Mod]={0};
int num=0,vv=0;
struct Hashtable
{
inline int hash(int x)
{
return abs(x)%Mod;
}
inline void insert(int x)
{
int value=hash(x);
if(version[value]!=vv)
{
version[value]=vv;
head[value]=-1;
}
//dic[num].w=x;
//dic[num].next=head[value];
dic[num]=(node){x,head[value]};
head[value]=num++;
}
inline bool find(int x)
{
int value=hash(x);
if(version[value]!=vv)
head[value]=-1;
for(int t=head[value];t!=-1;t=dic[t].next)
{
if(dic[t].w==x)return true;
}
return false;
}
void clear()
{
num=0;
vv++;
}
}table;
int main()
{
//freopen("input.txt","r",stdin);
//memset(head,-1,sizeof(head));
int T,i;
scanf("%d",&T);
while(T--)
{
table.clear(); //问题出在这里!!!
//num=0;vv=1; 这是我原来的写法!一定是错哪了吧!!
int n;
scanf("%d",&n);
while(n--)
{
int order;
scanf("%d",&order);
if(order==1)
{
int x,k;
scanf("%d%d",&x,&k);
for(i=0;i<k;i++)
{
table.insert(x+2*i);
}
}
else if(order==2)
{
int y;
scanf("%d",&y);
if(table.find(y))
printf("Yes\n");
else
printf("No\n");
}
else if(order==3)
table.clear();
}
}
return 0;
}
每个case都把版本号归零。。。这影响速度吗?!
诶?不对。。这会导致WA才对吧。。。
如果连续输几组不清空的数据
好像会让我出错。。。
哦呵呵。。。我试了,我错了,WA了
不想活了。。死了算了。。真的