Organize Your Train part II(hash)

http://poj.org/problem?id=3007

第一次用STL做的,TLE了,自己构造字符串哈希函数才可以。。

TLE代码:

 

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <string>
 5 #include <map>
 6 using namespace std;
 7 int main()
 8 {
 9     int n;
10     scanf("%d",&n);
11     while(n--)
12     {
13         int cnt = 0,j;
14         char ss[120];
15         string str,s,s1,s2;
16         map<string,int>v;
17         cin>>str;
18         s = str;
19         int len = str.size();
20 
21         for (int i = 1; i < len; i++)
22         {
23             s = str;
24             string::iterator it = s.begin();
25             for (j = 0; j < i; j++)
26                 ss[j] = str[j];
27             ss[j] = '';
28             s1 = ss;
29             s.erase(it,it+i);
30             s2 = s;
31             for (int k = 1; k <= 4; k++)
32             {
33                 if (v[s1+s2]==0)
34                 {
35                     v[s1+s2]++;
36                     cnt++;
37                 }
38                 if (v[s2+s1]==0)
39                 {
40                     v[s2+s1]++;
41                     cnt++;
42                 }
43                 if (k==1)
44                     reverse(s1.begin(),s1.end());
45                 else if (k==2)
46                     reverse(s2.begin(),s2.end());
47                 else if (k==3)
48                     reverse(s1.begin(),s1.end());
49             }
50         }
51         printf("%d
",cnt);
52     }
53     return 0;
54 }
View Code

AC代码:

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 const int N=100000;
 6 const int MOD=9467;
 7 int cnt = 0;
 8 
 9 struct node
10 {
11     char s[120];
12     struct node *next;
13 }*hash[N];
14 
15 void Hash(char *s1,char *s2)
16 {
17     char ss[120],*str;
18     strcpy(ss,s1);
19     strcat(ss,s2);
20     str = ss;
21     unsigned int key = 0;
22     while(*str)
23     {
24         key = key*131+(*str++);
25     }
26     key%=99991;
27     if (!hash[key])
28     {
29         hash[key] = new node;
30         strcpy(hash[key]->s,ss);
31         hash[key]->next = NULL;
32         cnt++;
33     }
34     else
35     {
36         struct node *p = hash[key];
37         if(strcmp(p->s,ss)==0)
38             return ;
39         while(p->next)
40         {
41             if (!strcmp(p->next->s,ss))
42                 return ;
43             p = p->next;
44         }
45         p->next = new node;
46         strcpy(p->next->s,ss);
47         p->next->next = NULL;
48         cnt++;
49     }
50 }
51 int main()
52 {
53 
54     char s[120],s1[120],s2[120],s3[120],s4[120];
55     int n,j;
56     scanf("%d",&n);
57     while(n--)
58     {
59         cnt = 0;
60         scanf("%s",s);
61         int len = strlen(s);
62         memset(hash,0,sizeof(hash));
63         for (int i = 1; i < len; i++)
64         {
65             for (j = 0; j < i; j++)
66                 s1[j] = s[j];
67             s1[j] = '';
68             for (j = i; j < len; j++)
69                 s2[j-i] = s[j];
70             s2[j-i] = '';
71             strcpy(s3,s1);
72             strcpy(s4,s2);
73             reverse(s1,s1+i);
74             reverse(s2,s2+len-i);
75             Hash(s3,s4);
76             Hash(s4,s3);
77             Hash(s1,s4);
78             Hash(s4,s1);
79             Hash(s2,s3);
80             Hash(s3,s2);
81             Hash(s1,s2);
82             Hash(s2,s1);
83 
84         }
85         printf("%d
",cnt);
86     }
87     return 0;
88 }
View Code

有关字符串哈希函数的经典算法

 

  1 unsigned int SDBMHash(char *str)
  2 {
  3     unsigned int hash = 0;
  4  
  5     while (*str)
  6     {
  7         // equivalent to: hash = 65599*hash + (*str++);
  8         hash = (*str++) + (hash << 6) + (hash << 16) - hash;
  9     }
 10  
 11     return (hash & 0x7FFFFFFF);
 12 }
 13  
 14 // RS Hash Function
 15 unsigned int RSHash(char *str)
 16 {
 17     unsigned int b = 378551;
 18     unsigned int a = 63689;
 19     unsigned int hash = 0;
 20  
 21     while (*str)
 22     {
 23         hash = hash * a + (*str++);
 24         a *= b;
 25     }
 26  
 27     return (hash & 0x7FFFFFFF);
 28 }
 29  
 30 // JS Hash Function
 31 unsigned int JSHash(char *str)
 32 {
 33     unsigned int hash = 1315423911;
 34  
 35     while (*str)
 36     {
 37         hash ^= ((hash << 5) + (*str++) + (hash >> 2));
 38     }
 39  
 40     return (hash & 0x7FFFFFFF);
 41 }
 42  
 43 // P. J. Weinberger Hash Function
 44 unsigned int PJWHash(char *str)
 45 {
 46     unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int) * 8);
 47     unsigned int ThreeQuarters    = (unsigned int)((BitsInUnignedInt  * 3) / 4);
 48     unsigned int OneEighth        = (unsigned int)(BitsInUnignedInt / 8);
 49     unsigned int HighBits         = (unsigned int)(0xFFFFFFFF) << (BitsInUnignedInt - OneEighth);
 50     unsigned int hash             = 0;
 51     unsigned int test             = 0;
 52  
 53     while (*str)
 54     {
 55         hash = (hash << OneEighth) + (*str++);
 56         if ((test = hash & HighBits) != 0)
 57         {
 58             hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
 59         }
 60     }
 61  
 62     return (hash & 0x7FFFFFFF);
 63 }
 64  
 65 // ELF Hash Function
 66 unsigned int ELFHash(char *str)
 67 {
 68     unsigned int hash = 0;
 69     unsigned int x    = 0;
 70  
 71     while (*str)
 72     {
 73         hash = (hash << 4) + (*str++);
 74         if ((x = hash & 0xF0000000L) != 0)
 75         {
 76             hash ^= (x >> 24);
 77             hash &= ~x;
 78         }
 79     }
 80  
 81     return (hash & 0x7FFFFFFF);
 82 }
 83  
 84 // BKDR Hash Function
 85 unsigned int BKDRHash(char *str)
 86 {
 87     unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
 88     unsigned int hash = 0;
 89  
 90     while (*str)
 91     {
 92         hash = hash * seed + (*str++);
 93     }
 94  
 95     return (hash & 0x7FFFFFFF);
 96 }
 97  
 98 // DJB Hash Function
 99 unsigned int DJBHash(char *str)
100 {
101     unsigned int hash = 5381;
102  
103     while (*str)
104     {
105         hash += (hash << 5) + (*str++);
106     }
107  
108     return (hash & 0x7FFFFFFF);
109 }
110  
111 // AP Hash Function
112 unsigned int APHash(char *str)
113 {
114     unsigned int hash = 0;
115     int i;
116  
117     for (i=0; *str; i++)
118     {
119         if ((i & 1) == 0)
120         {
121             hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));
122         }
123         else
124         {
125             hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));
126         }
127     }
128  
129     return (hash & 0x7FFFFFFF);
130 }
View Code

 

原文地址:https://www.cnblogs.com/lahblogs/p/3544611.html