魔咒詞典---哈希+二分

題目
哈利波特在魔法學校的必修課之一就是學習魔咒。據說魔法世界有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,想了半天最後看到某某某大佬的思路;
設兩個結構體類型的一個爲咒語,另一個爲功能,然後還需要兩個數組輔助,兩個數組分別表示咒語和功能。同時因爲咒語和功能的長度很小,所以兩個數組開二維;而兩個結構體變量存存放對應的hash值和位置(存位置是爲了輸出時方便);最後排序然後二分查找即可(註釋)。
代碼:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
#define ll long long
const unsigned int H=131;
const ll INF=1e9+7;
char s1[maxn][25],s2[maxn][85];
int cnt=1;
struct node{
    int h;
    int vis;
}zhouyu[maxn],gongneng[maxn];
bool cmp(node a,node b)
{
    return a.h<b.h;
}
unsigned int gethash(char *a)           //取字符串的hash值
{
    unsigned int sum=0;
    for(int i=0;a[i]!='\0';i++)
        sum=sum*H+a[i];
    return sum&0x7FFFFFFF;
}
void fun()                              //賦值,同時按hash排序,爲了二分
{
    for(int i=1;i<cnt;i++)
    {
        zhouyu[i].vis=i;            
        zhouyu[i].h=gethash(s1[i]);
        gongneng[i].vis=i;
        gongneng[i].h=gethash(s2[i]);
    }
    sort(zhouyu,zhouyu+cnt,cmp);
    sort(gongneng,gongneng+cnt,cmp);
}
int main()
{
    while(scanf("%s",s1[cnt]))
    {
        if(!strcmp(s1[cnt],"@END@"))
            break;
        getchar();
        gets(s2[cnt++]);
    }
    fun();
    int n;
    scanf("%d",&n);
    getchar();
    for(int i=1;i<=n;i++)
    {
        node t;
        char s[1010];
        gets(s);
        if(s[0]=='[')                       //如果爲咒語
        {
            t.h=gethash(s);
            int b=lower_bound(zhouyu,zhouyu+cnt,t,cmp)-zhouyu;      //二分查找是否有對應hash值
            if(zhouyu[b].h!=t.h)            //如果沒有對應功能
                printf("what?\n");
            else
                printf("%s\n",s2[zhouyu[b].vis]);           //輸出時應爲s2即功能數組;
        }
        else
        {
            t.h=gethash(s);
            int b=lower_bound(gongneng,gongneng+cnt,t,cmp)-gongneng;
            if(gongneng[b].h==t.h)
            {
                int len=strlen(s1[gongneng[b].vis]);            //這裏輸出的時候輸出咒語時去除[];
                s1[gongneng[b].vis][len-1]='\0';
                printf("%s\n",s1[gongneng[b].vis]+1);
            }
            else
                printf("what?\n");
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章