zoukankan      html  css  js  c++  java
  • 单词匹配

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

    题意 : 给你一个咒语对应着一个功能,要求有 q 此询问,对于每次询问给出相应的咒语后告诉你相应的功能
    思路分析:这题用 map 可能会超内存,因此我们这里用 hash 去做,对于 hash 后的值相同的情况下我们去连一条链,然后匹配的时候去在这条链上去匹配即可。
    代码示例:
    using namespace std;
    #define ll unsigned long long
    const ll maxn = 1e6+5;
    
    char s[200];
    struct node
    {
        char que[25];
        char ans[85];
        int next;
        
    }a[maxn], b[maxn];
    ll p = 100007;
    
    char que_[25], ans_[85];
    int ha[maxn], hb[maxn];
    ll cura=0, curb=0;
    
    ll gethash(char *str){
        ll res = 0;
        for(ll i = 0; *(str+i); i++){
            res = res*p+(str[i]-'a');
        }
        return res%p;
    }
    
    void insert(){
        ll hash_a = gethash(que_);
        strcpy(a[cura].que, que_);
        strcpy(a[cura].ans, ans_);
        a[cura].next = ha[hash_a];
        ha[hash_a] = cura;
        cura++;
         
        ll hash_b = gethash(ans_);
        strcpy(b[curb].que, que_);
        strcpy(b[curb].ans, ans_);
        b[curb].next = hb[hash_b];
        hb[hash_b] = curb;
        curb++;
        
        //printf("++++ %llu %llu 
    ", hash_a, hash_b);
    }
    
    bool searcha(char *str){
        ll num = gethash(str);
        int x= ha[num];
        
        //printf("1111111111   %llu %d
    ", num, x); 
        while(x != -1){
            //printf("_______ %s 
    ", a[x].que); 
            if (strcmp(a[x].que, str) == 0){
                printf("%s
    ", a[x].ans);
                return true;
            }
            x = a[x].next;
        }
        return false;
    }
    
    bool searchb(char *str){
        ll num = gethash(str);
        int x= hb[num];
        //printf("2222222222   %llu %llu
    ", num, x); 
        
        while(x != -1){
            
            if (strcmp(b[x].ans, str) == 0){
                printf("%s
    ", b[x].que);
                return true;
            }
            x = b[x].next;
        }
        return false;
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        memset(ha, -1, sizeof(ha));
        memset(hb, -1, sizeof(hb));
        while(1){
            gets(s+1);
            ll len = strlen(s+1);
            ll pos = 1;
            if (s[1] == '@') break;
            ll k = 0;
            for(ll i = 2; i <= len; i++){
                if (s[i] == ']') {pos = i; break;}
                que_[k++] = s[i];
            }
            que_[k] = '';
            k = 0;
            for(ll i = pos+2; i <= len; i++){
                ans_[k++] = s[i];
            }
            ans_[k] = '';
            insert();
        }
        ll q;
        
        cin >>q;
        getchar();
        while(q--){
            gets(s);
            ll len = strlen(s);
            if (s[0] == '['){
                s[len-1] = '';
                s[0] = '';
                if (!searcha(s+1)) printf("what?
    ");
            }
            else {
                if (!searchb(s)) printf("what?
    ");
            }
        }
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    GNU Linux中的SO_RCVLOWAT和SO_SNDLOWAT说明
    Spring.NET 的IOC(依赖注入)
    c# post 数据的方法
    C# .NET中的 反射的应用
    C# .NET修改注册表
    DataTabel DataSet 对象 转换成json
    sqlserver 行转列
    asp.net 二级域名session共享
    Asp.Net集群中Session共享
    微软企业库DBBA的研究
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/9380592.html
Copyright © 2011-2022 走看看