HDU 1880 题解(字符串哈希)

题面:

魔咒词典

Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14997 Accepted Submission(s): 3588

Problem Description
哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有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?

分析:

此题其实就是字符串哈希的模板题,将字符串s1映射到s2,输入s1求s2,输入s2求s1
第一反应是用STL,但此题用map会MLE或TLE,所以手写哈希表
字符串哈希的方法如下:
1.将前8位字母的ASCII码乘积作为key(用long long存),转换为数字的哈希问题
2.用数组和链表存储,key对应的字符串存在数组下标为key%c(c为常数)的地方
若key%c相同,则存在链表里,下图直观展示了该过程(图中数字为key)
这里写图片描述
3.查找时先到key对应的数组,如果元素不对,则继续遍历链表,直到查到该元素

代码实现:

先把字符串hash的代码发上来

const int c=10000;
struct myhash{//评测机上hash是关键字
    struct node {
        long long key;//若key%c相同辅助判重
        string s1;//若key相同辅助判重
        string s2;//要查找的东西,字符串s1对应的字符串s2
        node* next;//链表用指针
    }a[c+5];
    void push(long long x,string s1,string s2) {//插入函数
        if(a[x%c].key==0) {//如果数组x%c处没有元素,直接插入
            a[x%c].key=x;
            a[x%c].s1=s1;
            a[x%c].s2=s2;
        } else {//如果有,链表插入新节点(头插入)
            node *tmp=new node();
            tmp->key=x;
            tmp->s1=s1;
            tmp->s2=s2;
            tmp->next=a[x%c].next;//此处相当于head
            a[x%c].next=tmp;
        }
    }
    string found(long long x,string s1) {//查找函数
        if(a[x%c].key==x&&a[x%c].s1==s1) return a[x%c].s2;//如果字符串在数组里,直接返回对应的字符串s2
        if(a[x%c].key==0) return "what?";//如果数组里没有元素,链表里肯定也没有,返回what?
        else {
            node* tmp=a[x%c].next;//遍历链表查找
            while(tmp!=NULL) {
                if(tmp->key==x&&tmp->s1==s1) return tmp->s2;
                tmp=tmp->next;
            }
            return "what?";
        }
    }
};

全部代码如下,注意字符串处理:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int c=10000;
struct myhash{//评测机上hash是关键字
    struct node {
        long long key;//若key%c相同辅助判重
        string s1;//若key相同辅助判重
        string s2;//要查找的东西,字符串s1对应的字符串s2
        node* next;//链表用指针
    }a[c+5];
    void push(long long x,string s1,string s2) {//插入函数
        if(a[x%c].key==0) {//如果数组x%c处没有元素,直接插入
            a[x%c].key=x;
            a[x%c].s1=s1;
            a[x%c].s2=s2;
        } else {//如果有,链表插入新节点(头插入)
            node *tmp=new node();
            tmp->key=x;
            tmp->s1=s1;
            tmp->s2=s2;
            tmp->next=a[x%c].next;//此处相当于head
            a[x%c].next=tmp;
        }
    }
    string found(long long x,string s1) {//查找函数
        if(a[x%c].key==x&&a[x%c].s1==s1) return a[x%c].s2;//如果字符串在数组里,直接返回对应的字符串s2
        if(a[x%c].key==0) return "what?";//如果数组里没有元素,链表里肯定也没有,返回what?
        else {
            node* tmp=a[x%c].next;//遍历链表查找
            while(tmp!=NULL) {
                if(tmp->key==x&&tmp->s1==s1) return tmp->s2;
                tmp=tmp->next;
            }
            return "what?";
        }
    }
};
myhash H1,H2;//H1存储名字对应的内容,H2存储内容对应的名字
long long to_num(string str){//前8位的乘积hash 
    long long ans=1;
    int len=str.length();
    if(len>8) len=8;
    for(int i=0;i<len;i++) ans*=str[i];
    return ans;
}
int main() {
    int t; 
    string str,s1,s2;
    while(1) {
        int i;
        getline(cin,str);//有空格,必须用getline
        if(str=="@END@") break;
        for(i=0; i<str.length(); i++) {
            if(str[i]==']') break;//找到咒语名字和内容的分界点
        }
        s1=str.substr(0,i+1);
        s2=str.substr(i+2,str.length()-i-1);
        //将str分为两部分,s1是名字,s2是内容
        H1.push(to_num(s1),s1,s2);
        H2.push(to_num(s2),s2,s1);
    }
    cin>>t;
    getchar();//输入一个换行符,否则换行符会被getline读入到str中,导致在哈希表中查找'\n',多输出一个"what?"
    while(t--){
        getline(cin,str);
        bool have_kuo=false;//区分输入的是内容还是名字
        for(int i=0;i<str.length();i++){
            if(str[i]==']'){
                have_kuo=true;
                break;
            }
        } 
        string tmp;
        if(have_kuo) tmp=H1.found(to_num(str),str);
        else tmp=H2.found(to_num(str),str);
        if(tmp[0]=='['){
            for(int i=1;i<tmp.length()-1;i++)  cout<<tmp[i];//输出咒语名字时不带括号
            cout<<endl;
        }
        else cout<<tmp<<endl;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章