hdu 1075

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 1000003
char 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 1000003
char 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值