• ACM1880魔咒词典


    魔咒词典

    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?
     
      1 #include<iostream>
      2 #include<cstring>
      3 using namespace std;
      4 
      5 class Alphabet
      6 {
      7     public:
      8     char sign[30],power[100];
      9     Alphabet *next;
     10 };
     11 class Process
     12 {
     13     public:
     14         Process()
     15         {
     16             head=NULL;
     17             p=NULL;
     18             record=NULL;
     19         }
     20         void Input()
     21         {
     22             char sh[150];
     23             char s1[30],s2[100];
     24             while(gets(sh)&&(strcmp(sh,"@END@")!=0))
     25             {
     26                 char *ch=strchr(sh,']');
     27                 ch+=2;
     28                 int i;
     29                 for( i=0;sh[i]!=']';i++)
     30                 {
     31                     s1[i]=sh[i];
     32                 }
     33                 s1[i]=sh[i];
     34                 s1[i+1]=0;
     35                 Insert(s1,ch);
     36             }
     37             scanf("%d",&num);getchar();
     38             for(int i=1;i<=num;i++)
     39             {
     40                 char s[100];
     41                 gets(s);
     42                 if(s[0]=='[')
     43                 SearchSign(s);
     44                 else 
     45                 SearchPower(s);
     46             }
     47         }
     48         void SearchSign(char *s)
     49         {
     50             p=head;
     51             while(p!=NULL)
     52             {
     53                 if(strcmp(p->sign,s)==0)
     54                 {
     55                     printf("%s
    ",p->power);
     56                     return;
     57                 }
     58                 p=p->next;
     59             }
     60             printf("what?
    ");
     61         }
     62         void SearchPower(char *s)
     63         {
     64             p=head;
     65             while(p!=NULL)
     66             {
     67                 if(strcmp(p->power,s)==0)
     68                 {
     69                     int n=strlen(p->sign);
     70                     for(int i=1;i<n-1;i++)
     71                     {
     72                         printf("%c",p->sign[i]);
     73                     }
     74                     printf("
    ");
     75                     return;
     76                 }
     77                 p=p->next;
     78             }
     79             printf("what?
    ");
     80         }
     81         void Insert(char *s1,char *s2)
     82         {
     83             p=new Alphabet;
     84             strcpy(p->sign,s1);
     85             strcpy(p->power,s2);
     86             if(head==NULL)
     87             head=p;
     88             else
     89             record->next=p;
     90             record=p;
     91             record->next=NULL;
     92         }
     93         void OUTPUTALL()
     94         {
     95             p=head;
     96             while(p!=NULL)
     97             {
     98                 cout<<p->sign<<" "<<p->power<<endl;
     99                 p=p->next;
    100             }
    101             //printf("what?
    ");
    102         }
    103     private:
    104         int num;
    105         Alphabet *head,*p,*record;
    106 };
    107 int main()
    108 {
    109     while(1)
    110     {
    111         Process p1;
    112         p1.Input();
    113     }
    114     return 0;
    115 }

    [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@
    8
    [lumos]
    the summoning charm
    [arha]
    take me to the sky
    the memory charm
    send a Patronus to the dementors
    [tarantallegra]
    send a jet of silver light to hit the enemy

    我所提交的代码和这个不同,需要将类撤掉,把输入放到主函数里,不然无法结束,但是整体算法不变;

    What I don't dare to say is I can't!
  • 相关阅读:
    MyBatis缓存机制-二级缓存
    hibernate异常之--count查询异常
    mysql聚合函数操作
    spring-知识小结之注解为属性赋值
    spring整合问题分析之-Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
    hibernate detached分离查询 与 抓取策略注意事项
    *.hbm.xml映射文件的元素及属性
    hibernate自动建表之engine设置
    Day1 summary
    A problem needed to review and fix later
  • 原文地址:https://www.cnblogs.com/sytu/p/3871580.html
Copyright © 2020-2023  润新知