# 【JZOJ 1520】破碎的路径（hash）

SwimmingPool OldTree
BirdsNest Garage
Garage SwimmingPool

3
SwimmingPool OldTree
BirdsNest Garage
Garage SwimmingPool

BirdsNest Garage
Garage SwimmingPool
SwimmingPool OldTree

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
#define maxn 12006
#define mo 56399
int f[maxn],t[maxn],next[mo],n,s;
string h[mo],st1[maxn],st2[maxn];
bool p[mo];

int hash(string st)
{
int x=0,k=1,l=st.length(),xx=0;
for (int i=0;i<l;i++)
{
x=x+(st[i]*k) % mo;
k=k*10 % mo;
}
while (h[(x+xx) % mo]!="" && h[(x+xx) % mo]!=st)
xx++;
return (x+xx) % mo;
}

bool find(string st)
{
if (h[hash(st)]==st)
return 1;
else
return 0;
}

int main()
{
char c;
scanf("%d",&n);
scanf("%c",&c);
for (int i=0;i<n;i++)
{
cin>>st1[i]>>st2[i];
scanf("%c",&c);
if (c=='\n')
{
if (find(st1[i]))
p[hash(st1[i])]=1;
else
h[hash(st1[i])]=st1[i];
if (find(st2[i]))
p[hash(st2[i])]=1;
else
h[hash(st2[i])]=st2[i];
f[i+1]=hash(st1[i]);
t[i+1]=hash(st2[i]);
next[f[i+1]]=i+1;
}
}
for (int i=0;i<n;i++)
if (p[hash(st1[i])]==0)
s=hash(st1[i]);
int i=next[s];
while (i>0)
{
cout<<h[f[i]]<<' '<<h[t[i]]<<endl;
i=next[t[i]];
}
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120