• POJ 2503


    http://poj.org/problem?id=2503

    题意就是字典翻译。这个输入输出真心恶心,要不是看discuss我肯定是解决不了,还用上了sscanf函数。。。。

    这道题我用几种方法做。

    sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源。

    一 qsort+二分

     1 //Memory:5060K  c++run time:422MS 
     2 #include <stdio.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <string.h>
     6 #include <stdlib.h>
     7 
     8 using namespace std;
     9 
    10 
    11 struct sm{
    12     char dic[25];
    13     char eng[25];
    14 }str[100010];
    15 
    16 char st[30],a[30];
    17 
    18 int cmp(const void *a,const void *b)
    19 {
    20     return strcmp((*(sm*)a).dic,(*(sm*)b).dic);
    21 }
    22 
    23 int main()
    24 {
    25     int n=0;
    26     while(gets(a)){
    27         if(a[0] == '') break;
    28         sscanf(a,"%s%s",str[n].eng,str[n].dic);
    29         n++;
    30     }
    31     qsort(str,n,sizeof(str[0]),cmp);
    32     while(gets(st)&&st[0]!='')
    33     {
    34         int low=0,high=n,flag=0,mid;
    35         while(low<=high)
    36             {
    37               mid=(low+high)/2;
    38               if(!strcmp(st,str[mid].dic))
    39               {
    40                   flag=1;
    41                   printf("%s
    ",str[mid].eng);
    42                   break;
    43               }
    44               else
    45               if(strcmp(st,str[mid].dic)>0)
    46                low=mid+1;
    47               else
    48                 if(strcmp(st,str[mid].dic)<0)
    49                 high=mid-1;
    50             }
    51         if(flag==0) printf("eh
    ");
    52     }
    53     return 0;
    54 }

    二、用map

    这是我第一次用map这个函数,这也只是比较水,当然,这个题也比较水,map有个好处就是他会对里面所映射的元素进行排序

    其中count是查找这个元素是否存在这个Map中

     1 Memery:9624K   c++ runtime:688MS 
     2 #include <stdio.h>
     3 #include <iostream>
     4 #include <map>
     5 #include <string>
     6 
     7 using namespace std;
     8 
     9 map<string,string>mp;
    10 
    11 int main()
    12 {
    13     char a[30],b[15],c[15];
    14     while(gets(a)&&a[0]!='')
    15     {
    16         sscanf(a,"%s%s",b,c);
    17         mp[c]=b;
    18     }
    19     while(gets(a)&&a[0]!='')
    20     {
    21         if(!mp.count(a)) cout<<"eh"<<endl;
    22         else cout<<mp[a]<<endl;
    23     }
    24     return 0;
    25 }

    三、Hash

  • 相关阅读:
    《代码整洁之道》(Clean Code) 读书笔记
    设计模式的征途—19.命令(Command)模式
    位运算
    二分查找法(递归和非递归算法)
    二进制补习--原码 反码 补码
    CentOS7配置mysql8免安装版
    Centos7安装Python3的方法
    CentOS7笔记本合盖不待机
    最长回文子串
    多数元素
  • 原文地址:https://www.cnblogs.com/Tree-dream/p/5506656.html
Copyright © 2020-2023  润新知