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
- HDU 1075
- hdu 1075
- hdu 1075
- hdu 1075
- 1075(hdu)
- hdu 1075
- HDU 1075
- hdu 1075
- hdu 1075
- HDU 1075
- HDU 1075
- Hdu 1075
- Hdu 1075+ Hdu 1251 (动态Trie)
- [字典树]hdu 1075
- hdu 1075 字典树
- 字典树 hdu 1075
- hdu 1075 tire树
- hdu 1075 map
- linux s3c24xx如何通过i2c发送数据.
- OCP 1Z0 053 95
- DC设备描述符表
- 模态对话框 vs 非模态对话框
- 性能测试之性能计数器和监测工具
- hdu 1075
- C#开发Android-文件的读写
- 写了一个程序,分析各个源文件之间的include关系。
- POJ 3176:Cow Bowling
- Android写通讯录 用到的数据库知识
- hdu 1241 Oil Deposits
- 剑指offer面试题13扩展:带头指针的单链表的操作
- 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。
- Etherpeek监听UDP报文时间戳获取