zoukankan      html  css  js  c++  java
  • 字符串魔咒词典

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1880

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

    Author

    ZJU

    开始由于,内存太小,没仔细看结果就没到那个个数,wa好几次,内存开太大,结果就超内存,呀呀,所以要仔细阅读题目,尽量做到适可而止;只好适当减少点,就AC过了。最直观就是遍历比较,有相等的则输出对应的字符串。

    之前看到类似的字典对,有用sort()函数 + bsearch()查找处理的方法;提交了两个代码,明显先排序,再二分的速度快很多~~~

    函数名: bsearch()函数用法 
    
    功 能: 二分法搜索 
    
    用 法: void *bsearch(const void *key, const void *base, size_t nelem, size_t width, int(*fcmp)(const void *, const *)); 
    
    语法: 
    
    #include <stdlib.h> void *bsearch( const void *key, const void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) ); 
    
    参数:第一个:要查找的关键字。第二个:要查找的数组。第三个:指定数组中元素的数目。第四个:每个元素的长度(以字符为单位)。第五个:指向比较函数的指针。 
    
    功能: 函数用折半查找法在从数组元素buf[0]到buf[num-1] 匹配参数key。如果函数compare 的第一个参数小于第二个参数,返回负值;如果等于返回零值;如果大于返回正值。数组buf 中的元素应以升序排列。函数bsearch()的返回值是指向匹配项,如果没有发现匹配项,返回NULL
    //sort函数 + bsearch查找
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    
    typedef struct MY_WORD
    {
        char mozhou[25];
        char m_function[85];
    };
    
    MY_WORD my_word1[100005];
    MY_WORD my_word2[100005];
    
    int cmp1(const void *elem1 , const void *elem2)
    {
        return strcmp(((MY_WORD *)elem1)->mozhou,((MY_WORD*)elem2)->mozhou);
    }
    int cmp2(const void *elem1,const void *elem2)
    {
        return strcmp(((MY_WORD *)elem1)->m_function,((MY_WORD*)elem2)->m_function);
    }
    int main()
    {
        char s[100];
        MY_WORD *ans,tmp;
        gets(s);
        int n =0;
        int j,k,t;
        int case_number;
        while(strcmp(s,"@END@")!=0)
        {
            j =k= t=0;
            if(s[j]=='[')
                {
                    j++;
                    while(s[j]!=']'&& s[j]!=0)
                    {
                        my_word1[n].mozhou[k++] = s[j];
                        j++;
                    }
                    my_word1[n].mozhou[k] =0;
                    j+=2;
                    while(s[j]!=0)
                    {
                        my_word1[n].m_function[t++] = s[j];
                        j++;
                    }
                    my_word1[n].m_function[t] = 0;
                }
            strcpy(my_word2[n].mozhou ,my_word1[n].mozhou);
            strcpy(my_word2[n].m_function ,my_word1[n].m_function);
            n++;
            gets(s);
        }
        qsort(my_word1,n,sizeof(MY_WORD),cmp1);
        qsort(my_word2,n,sizeof(MY_WORD),cmp2);
        scanf("%d",&case_number);
        getchar();
        for(;case_number>=1;case_number--)
        {
            gets(s);
            memset(&tmp,0,sizeof(MY_WORD));
            if(s[0] =='[')
            {
                int i =1;
                k =0;
                while(s[i]!=']')
                {
                    tmp.mozhou[k++] = s[i++];
                }
                tmp.mozhou[k] =0;
                ans = (MY_WORD *)bsearch(&tmp,my_word1,n,sizeof(MY_WORD),cmp1);
                if(ans!=NULL)
                    printf("%s\n",ans->m_function);
                else
                    printf("what?\n");
            }
            else
            {
                int i =0;
                k =0;
                while(s[i]!=0)
                {
                    tmp.m_function[k++] = s[i++];
                }
                tmp.m_function[k] =0;
                ans = (MY_WORD *)bsearch(&tmp,my_word2,n,sizeof(MY_WORD),cmp2);
                if(ans!=NULL)
                    printf("%s\n",ans->mozhou);
                else
                    printf("what?\n");
            }
                
        }
        return 0;
    }
    //直观解法,遍历
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    
    typedef struct MY_WORD
    {
        char mozhou[25];
        char m_function[85];
    };
    
    MY_WORD my_word1[100005];
    int main()
    {
        char s[100];
        MY_WORD *ans,tmp;
        gets(s);
        int n =0;
        int j,k,t;
        int case_number;
        while(strcmp(s,"@END@")!=0)
        {
            j =k= t=0;
            if(s[j]=='[')
                {
                    j++;
                    while(s[j]!=']'&& s[j]!=0)
                    {
                        my_word1[n].mozhou[k++] = s[j];
                        j++;
                    }
                    my_word1[n].mozhou[k] =0;
                    j+=2;
                    while(s[j]!=0)
                    {
                        my_word1[n].m_function[t++] = s[j];
                        j++;
                    }
                    my_word1[n].m_function[t] = 0;
                }
            n++;
            gets(s);
        }
        scanf("%d",&case_number);
        getchar();
        for(;case_number>=1;case_number--)
        {
            gets(s);
            int flag =0;
            memset(&tmp,0,sizeof(MY_WORD));
            if(s[0] =='[')
            {
                int i =1;
                k =0;
                while(s[i]!=']')
                {
                    tmp.mozhou[k++] = s[i++];
                }
                tmp.mozhou[k] =0;
                for(int tt =0;tt < n;tt++)
                    if(strcmp(tmp.mozhou,my_word1[tt].mozhou)==0)//遍历比较                
                    {
                        printf("%s\n",my_word1[tt].m_function);
                        flag =1;
                        break;
                    }
            }
            else
            {
                int i =0;
                k =0;
                while(s[i]!=0)
                {
                    tmp.m_function[k++] = s[i++];
                }
                tmp.m_function[k] =0;
                for(int tt =0;tt < n;tt++)
                    if(strcmp(tmp.m_function,my_word1[tt].m_function)==0)
                    {
                        printf("%s\n",my_word1[tt].mozhou);
                        flag =1;
                        break;
                    }
            }
            if(flag ==0)
                printf("what?\n");
        }
        return 0;
    }
  • 相关阅读:
    基础--补习汇编笔记--1
    SpProcPool阅读笔记--1
    一般树--common tree
    code-reading-notes--xml 解析
    code-reading-notes--libyang-1
    linux--rbtree 解惑 insert
    记录一次手动杀毒过程
    B-Tree概念
    db2 -- 存储过程01
    sql server 带输入输出参数的分页存储过程(效率最高)
  • 原文地址:https://www.cnblogs.com/cheng07045406/p/3131011.html
Copyright © 2011-2022 走看看