HDU1880 spell dictionary

Title effect: an input corresponding to a plurality of rows, each row two strings, the two strings are mapped to each other. The next time inquiry, if the string occurred, outputs the corresponding character string.

Analysis: double hash to determine whether the presence of the string, the string corresponding output line. Double hash entry title, string quite interesting.

Code:

#include<iostream>
#include<string>
#include<map>
#include<string>
using namespace std;
typedef unsigned long long ull;
map<pair<int,int>,string> dict;
const int s1=133,s2=233;
const int maxn=1e5+7;
char s[1000],t[1000];
int hash1(char* s){
    int ans=0;
    for(int i=0;s[i];i++)
        years = (s1 + years * s [i])% maxn;
    return years;
}
int hash2(char* s){
    int ans=0;
    for(int i=0;s[i];i++)
        years = (years * s2 + s [i])% maxn;
    return years;
}
void read(){
    int cnt=0;
    while(scanf("%s",s)&&s[0]!='@'){
        getchar();
        cnt=0;
        while((t[cnt]=getchar())!='\n')
            cnt++;
        T [cut] = ' \ 0 ' ;
        dict[make_pair(hash1(s),hash2(s))]=t;
        dict[make_pair(hash1(t),hash2(t))]=s;
    }
}
void solve(){
    int m,x,y,cnt=0;
    scanf("%d",&m);
    getchar();
    while(m--){
        cnt=0;
        while((s[cnt]=getchar())!='\n')
            cnt++;
        s[cnt]='\0';
        x=hash1(s);
        y=hash2(s);
        if(s[0]=='['){
            if(dict.find(make_pair(x,y))==dict.end()) printf("what?\n");
            else cout<<dict[make_pair(x,y)]<<endl;
        }
        else{
            if(dict.find(make_pair(x,y))==dict.end()) printf("what?\n");
            else cout<<dict[make_pair(x,y)].substr(1,dict[make_pair(x,y)].length()-2)<<endl;
        }
    }
}
int main () {
    read();
    
    solve();
   
    return 0;
}

Guess you like

Origin www.cnblogs.com/SwiftAC/p/12153073.html