• poj2503 哈希


    这题目主要是难在字符串处理这块。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 #define mod 999983
     5 struct node{
     6     char str[10+100];
     7     char res[10+100];
     8     node *next;
     9 }p[mod];
    10 int sign[mod];
    11 void hash(char str[],char res[]){
    12     int i,l=strlen(str);
    13     int key=0;
    14     for(i=0;i<l;++i)    key=(key+str[i]*str[i])%mod;
    15     if(sign[key]==0){
    16         strcpy( p[key].str , str );
    17         strcpy( p[key].res , res );
    18         p[key].next=NULL;
    19         sign[key]=1;
    20     } else if( sign[key]==1 ){
    21         node *op=&p[key];
    22         while(op->next!=NULL)
    23             op=op->next;
    24         node *tmp=(node *)malloc(sizeof(node));
    25         strcpy( tmp->str,str );
    26         strcpy( tmp->res,res );
    27         tmp->next=NULL;
    28         op->next=tmp;
    29     }
    30     return ;
    31 }
    32 node *find_hash(char str[]){
    33     int i,l=strlen(str);
    34     int key=0;
    35     for(i=0;i<l;++i)    key=(key+str[i]*str[i])%mod;
    36     if(sign[key]==0)   return NULL;
    37     else if(sign[key]==1){
    38        // printf("here!
    ");
    39         node *op=&p[key];
    40         while(1){
    41             if(strcmp(str,op->str)==0)
    42                 return op;
    43             if(op->next==NULL) break;
    44             op=op->next;
    45         }
    46     }
    47     return NULL;
    48 }
    49 
    50 int change(){
    51     char str[10+100];
    52     int l,i,j;
    53     gets(str);
    54     if(str[0]=='')    return 0;
    55     l=strlen(str);
    56 
    57     char str1[10+100],str2[10+100];
    58     str2[0]=str[0];
    59     for(i=1;i<l;++i){
    60         if(str[i]==' '){
    61             break;
    62         }
    63         str2[i]=str[i];
    64     }
    65     str2[i]='';
    66     i++;
    67     str1[0]=str[i++];
    68     for(j=1;i<l;++i,++j){
    69         str1[j]=str[i];
    70     }
    71     str1[j]='';
    72     if(strlen(str1)>10) return 1;
    73     hash(str1,str2);
    74     //printf("%s,%s
    ",str1,str2);
    75     return 1;
    76 
    77 
    78 }
    79 int main(){
    80     char str[10+100];
    81     memset(sign,0,sizeof(0));
    82     while(change()!=0);
    83     while(~scanf("%s",str)){
    84         if(find_hash(str)==NULL) printf("eh
    ");
    85         else
    86         printf("%s
    ",find_hash(str)->res);
    87     }
    88     return 0;
    89 }
  • 相关阅读:
    Ajax工作原理和原生JS的ajax封装
    HNU 13073 Ternarian Weights 解题报告
    如何在Eclipse中配置python开发环境
    C++中vector 容器的基本操作
    2014年百度实习生面试题及总结
    Python计算一个项目中含有的代码行数
    Linux环境下的GCC编译器与GDB调试工具介绍
    linux环境下Vim的配置
    计算机网络中好的期刊和会议
    hdu 1005解题报告
  • 原文地址:https://www.cnblogs.com/symons1992/p/3488396.html
Copyright © 2020-2023  润新知