HDU 1880 魔咒詞典(Hash 二分)

2021-09-12 01:19:18 字數 2279 閱讀 2446

哈利波特在魔法學校的必修課之一就是學習魔咒。據說魔法世界有100000種不同的魔咒,哈利很難全部記住,但是為了對抗強敵,他必須在危急時刻能夠呼叫任何乙個需要的魔咒,所以他需要你的幫助。 

給你一部魔咒詞典。當哈利聽到乙個魔咒時,你的程式必須告訴他那個魔咒的功能;當哈利需要某個功能但不知道該用什麼魔咒時,你的程式要替他找到相應的魔咒。如果他要的魔咒不在詞典中,就輸出「what?」

input

首先列出詞典中不超過100000條不同的魔咒詞條,每條格式為: 

[魔咒] 對應功能 

其中「魔咒」和「對應功能」分別為長度不超過20和80的字串,字串中保證不包含字元「[」和「]」,且「]」和後面的字串之間有且僅有乙個空格。詞典最後一行以「@end@」結束,這一行不屬於詞典中的詞條。 

詞典之後的一行包含正整數n(<=1000),隨後是n個測試用例。每個測試用例佔一行,或者給出「[魔咒]」,或者給出「對應功能」。

output

每個測試用例的輸出佔一行,輸出魔咒對應的功能,或者功能對應的魔咒。如果魔咒不在詞典中,就輸出「what?」

sample input

[expelliarmus] the disarming charm

[rictusempra] send a jet of silver light to hit the enemy

[tarantallegra] control the movement of one's legs

[serpensortia] shoot a snake out of the end of one's wand

[lumos] light the wand

[obliviate] the memory charm

[expecto patronum] send a patronus to the dementors

[accio] the summoning charm

@end@

4[lumos]

the summoning charm

[arha]

take me to the sky

sample output

light the wand

accio

what?

what?

中文題,題意就不用說了。

題解:現在我們定義兩個結構體陣列,儲存字串轉換後的值,和對應編號。我們提前將「魔咒」和「對應功能」的hash值算出來然後儲存下來,然後按hash值從小到大排序,然後後面輸入需要查詢的,直接去對應陣列二分查詢就好了,思想很好理解,注意:要是輸入的對應功能,要求輸出魔咒時,沒有那個中括號,細節看**。

#include #include#include#include#include#include#include#include#include#includeconst int maxn=1e5+10;

const int mod=1e9+7;

const int inf=1e8;

#define me(a,b) memset(a,b,sizeof(a))

#define lowbit(x) x&(-x)

#define mid l+(r-l)/2

#define lson l,mid,rt<<1

#define rson mid+1,r,rt<<1|1

#define pi 3.14159265358979323846

int dir[4][2]= ;

typedef long long ll;

using namespace std;

char str1[maxn][30],str2[maxn][100];

int len=0;

struct node

}cnt1[maxn],cnt2[maxn];///cnt1儲存魔咒的hash值,cnt2儲存對應功能的hash值

int gethash(char *str)///算乙個字串的hash值

void solve()///將魔咒和對應功能的字串轉化成相應的hash值

sort(cnt1,cnt1+len);///排序,方便後面二分查詢。

sort(cnt2,cnt2+len);

}int main()

solve();

int n;

scanf("%d",&n);

getchar();

for(int i=0;ielse

else

printf("what?\n");}}

return 0;

}

hdu1880(魔咒詞典)

1.下面是二分查詢 ac include include include define max 100005 typedef struct node node node mag1 max node mag2 max int cmp1 const void a,const void b int cmp...

魔咒詞典 HDU 1880

感覺這題巨毒瘤,讀入字串方面調了好久才避免了讀入空白字元。思路就是對每條資訊的魔咒和功能的記錄在s1和s2串裡,並在ihash陣列裡通過資訊的編號 cnt 確定存放的列,將魔咒 魔咒的hash值存在第一行 或功能 功能的hash值存在第2行 的hash值存入。然後輸入乙個要查詢的字串,就先求出其ha...

HDU 1880魔咒詞典

problem description 哈利波特在魔法學校的必修課之一就是學習魔咒。據說魔法世界有100000種不同的魔咒,哈利很難全部記住,但是為了對抗強敵,他必須在危急時刻能夠呼叫任何乙個需要的魔咒,所以他需要你的幫助。給你一部魔咒詞典。當哈利聽到乙個魔咒時,你的程式必須告訴他那個魔咒的功能 當...