• hdu 1880 魔咒词典(字符串hash)


    题目链接:hdu 1880 魔咒词典

    题意:

    给你一个10w的词典,让你输出对应的字段。

    题解:

    map暴力存字符串肯定会卡内存,这里用BKDR字符串hash一下,用map映射一下。

     1 #include<bits/stdc++.h>
     2 #define F(i,a,b) for(int i=a;i<=b;++i)
     3 using namespace std;
     4 typedef unsigned long long ull;
     5 
     6 const int N=1e5+7,seed=1331;
     7 char tmp[200],str[200],mo[N][25],gong[N][90];
     8 int ed,n;
     9 map<ull,int>cnt1;
    10 map<ull,int>cnt2;
    11 
    12 inline int idx(char x)
    13 {
    14     if(x==' ')return 1;
    15     return x-'a'+2;
    16 }
    17 
    18 ull ask(char *s)
    19 {
    20     ull now=0;
    21     for(int i=0;s[i]!=0;i++)
    22     {
    23         now+=now*seed+idx(s[i]);
    24     }
    25     return now;
    26 }
    27 
    28 void ins()
    29 {
    30     int now=0;
    31     for(int i=1;tmp[i]!=0;i++)
    32     {
    33         if(tmp[i]==']')
    34         {
    35             str[now]=0;
    36             cnt1[ask(str)]=++ed;
    37             strcpy(mo[ed],str);
    38             now=0;
    39             i+=2;
    40         }
    41         str[now++]=tmp[i];
    42     }
    43     str[now]=0;
    44     cnt2[ask(str)]=ed;
    45     strcpy(gong[ed],str);
    46 }
    47 
    48 int main()
    49 {
    50     while(gets(tmp))
    51     {
    52         cnt1.clear(),cnt2.clear();
    53         ed=0;
    54         ins();
    55         while(1)
    56         {
    57             gets(tmp);
    58             if(tmp[0]=='@')break;
    59             ins();
    60         }
    61         scanf("%d",&n);
    62         getchar();
    63         F(i,1,n)
    64         {
    65             gets(tmp);
    66             if(tmp[0]=='[')
    67             {
    68                 int now=0;
    69                 for(int j=1;tmp[j]!=']';j++)str[now++]=tmp[j];
    70                 str[now]=0;
    71                 ull tp=ask(str);
    72                 int cur=cnt1[tp];
    73                 if(cur)printf("%s
    ",gong[cur]);
    74                 else puts("what?");
    75             }else
    76             {
    77                 ull tp=ask(tmp);
    78                 int cur=cnt2[tp];
    79                 if(cur)printf("%s
    ",mo[cur]);
    80                 else puts("what?");
    81             }
    82         }
    83     }
    84     return 0;
    85 }
    View Code
  • 相关阅读:
    iOS TTF文件改变字体
    iOS CoreAnimation 核心动画
    iOS no visible @interface for 'UIButton' declares the selector errors
    iOS 如何通过CocoaPods添加第三方框架
    iOS AVAudioPlayer播放音乐
    iOS 一些常用代码的总结
    iOS 基础
    qworker 实例
    delphi RTTI 反射技术
    delphi IOUtils单元 处理文件路径相关
  • 原文地址:https://www.cnblogs.com/bin-gege/p/6366297.html
Copyright © 2020-2023  润新知