hdu 1075

来源:互联网 发布:数据透视分析表怎么用 编辑:程序博客网 时间:2024/05/29 13:38

hdu 1075


题意:

就是用将火星文用英语代替。


解题思路:

利用字符串hash或map可求


注意:

//map 解法#include <stdio.h>#include <string.h>#include <stdlib.h>#include <ctype.h>#include <math.h>#include <string>#include <iostream>#include<map>#include <algorithm>using namespace std;map<string,string>mp;map<string,string>::iterator itr;#define MAXN 1000013#define Mod 1000003char str1[MAXN],str2[MAXN];char hashstr[MAXN][20];bool vis[MAXN];unsigned int code[MAXN];inline unsigned int BKDRHash(char *str){    unsigned int seed = 131; // 31 131 1313 13131 131313 etc..    unsigned int hash = 0;        while (*str)    {        hash = hash * seed + (*str++);    }        return (hash & 0x7FFFFFFF);}void hashit(char *key,char *val){    //    unsigned int hashnum = BKDRHash(key);    //    int ind = hashnum%Mod;    //    while(vis[ind]&&code[ind]!=hashnum){    //        ind+=10;    //        ind%=Mod;    //    }    //    code[ind] = hashnum;    //    vis[ind] = true;    //    strcpy(hashstr[ind],val);    mp.insert(pair<string,string>(key,val));}void getstr(unsigned int hashnum,char *tmp){    //    int ind=hashnum%Mod;    //    tt = ind;    //    while(1){    //        if(vis[ind]&&code[ind]==hashnum){    //            printf("%s",hashstr[ind]);    //            return;    //        }    //       //00 break;    //        ind+=10;    //        ind%=Mod;    //        //tt++;    //        if(ind>tt)break;    //    }        if((itr=mp.find(tmp))!=mp.end()){        cout<<itr->second;        return;    }    printf("%s",tmp);}void output(char *word){    char tmp[MAXN];    int len = 0;    while((*word)!=0){        if(isalpha((*word))){            tmp[len++] = (*word);        }        else {            if(len>0){                tmp[len]=0;                unsigned int hashnum = BKDRHash(tmp);                getstr(hashnum,tmp);                len = 0;            }            putchar((*word));        }        word++;    }    if(len>0){        tmp[len]=0;        unsigned int hashnum = BKDRHash(tmp);        getstr(hashnum,tmp);        len = 0;    }    printf("\n");}int main(){    //    for(int i=1000001;;i++){    //        bool flag = true;    //        for(int j=2;j<=sqrt(i*1.0);j++){    //            if(i%j==0){    //                flag = false;    //                break;    //            }    //        }    //        if(flag){    //            printf("%d\n",i);    //            break;    //        }    //    }    // int flag1=0;    scanf("%s",str1);        //memset(vis,false,sizeof(vis));    while(scanf("%s",str1)&&strcmp(str1, "END")!=0){        scanf("%s",str2);        hashit(str2,str1);    }        getchar();    // getchar();    scanf("%s",str1);    getchar();    while(gets(str1)&&strcmp(str1,"END")){        output(str1);    }        // flag1++;    //flag1%=2;        return 0;}

//字符串hash#include <stdio.h>#include <string.h>#include <stdlib.h>#include <ctype.h>#include <math.h>#include <vector>#include <string>using namespace std;#define MAXN 1000013#define Mod 1000003char str1[MAXN],str2[MAXN];typedef struct node{    char key[20];    char val[20];    node(){    }    node (char * nn,char *ss){        //hashnum = nn;        strcpy(key,nn);        strcpy(val, ss);    }};vector<node>vt[MAXN];inline unsigned int BKDRHash(char *str){    unsigned int seed = 31; // 31 131 1313 13131  etc..    unsigned int hash = 0;        while (*str)    {        hash = hash * seed + (*str++);    }        return (hash & 0x7FFFFFFF);}void hashit(char *key,char *val){    unsigned int hashnum = BKDRHash(key);    int ind = hashnum%Mod;    vt[ind].push_back(node(key,val));    //strcpy(hashstr[ind],val);}void getstr(unsigned int hashnum,char *tmp){    int ind=hashnum%Mod;    int tt=0;    for(int i=0;i<vt[ind].size();++i){        if(strcmp(vt[ind][i].key,tmp)==0){            printf("%s",vt[ind][i].val);            return;        }    }    printf("%s",tmp);}void output(char *word){    char tmp[MAXN];    int len = 0;    while((*word)!=0){        if(isalpha((*word))){            tmp[len++] = (*word);        }        else {            if(len>0){                tmp[len]=0;                unsigned int hashnum = BKDRHash(tmp);                getstr(hashnum,tmp);                len = 0;            }            putchar((*word));        }        word++;    }    if(len>0){        tmp[len]=0;        unsigned int hashnum = BKDRHash(tmp);        getstr(hashnum,tmp);        len = 0;    }    printf("\n");}int main(){    //    for(int i=1000001;;i++){    //        bool flag = true;    //        for(int j=2;j<=sqrt(i*1.0);j++){    //            if(i%j==0){    //                flag = false;    //                break;    //            }    //        }    //        if(flag){    //            printf("%d\n",i);    //            break;    //        }    //    }    // int flag1=0;    scanf("%s",str1);        //memset(vis,false,sizeof(vis));    while(scanf("%s",str1)&&strcmp(str1, "END")!=0){        scanf("%s",str2);        hashit(str2,str1);    }        getchar();    // getchar();    scanf("%s",str1);    getchar();    while(gets(str1)&&strcmp(str1,"END")){        output(str1);    }        // flag1++;    //flag1%=2;        return 0;}


0 0
原创粉丝点击