"Template" string hash

Source: Internet
Author: User
Tags first string

Title Description

Title, given n strings (the length of the I string is MI, the string contains numbers, uppercase and lowercase letters, case-sensitive), how many different strings are requested for n strings.

Friendly reminder: If you really want to practice the hash, please consciously, otherwise please turn right to PJ Test field:) input and output format

Input format:

The first line contains an integer n, which is the number of strings.

The next n rows each row contains a string for the supplied string.

Output format:

The output contains a row containing an integer that is the number of different strings.

Input and Output Sample input example # #:
5abcaaaaabcabcc12345
Sample # # of output:
4
Description

Time limit: 1000ms,128m

Data size:

For 30% of data:n<=10,mi≈6,mmax<=15;

For 70% data: n<=1000,mi≈100,mmax<=150

For 100% data: n<=10000,mi≈1000,mmax<=1500

Sample Description:

The first string (ABC) in the sample is the same as the third string (ABC), so the collection of the supplied string is {aaaa,abc,abcc,12345}, so there is a total of 4 different strings.

TIP: If you are interested, you can take a look at the following three questions:

bzoj3097:http://www.lydsy.com/judgeonline/problem.php?id=3097

bzoj3098:http://www.lydsy.com/judgeonline/problem.php?id=3098

bzoj3099:http://www.lydsy.com/judgeonline/problem.php?id=3099

If you have studied it carefully (or at least the AC number), I think you will understand the correct posture of the string hash ^_^

The simplest hash, after 80 minutes.

Code:

1#include <cstdio>2#include <cstring>3 using namespacestd;4 Const intMod=88782431;5 intN,l,ans;6UnsignedintHash;7 BOOLv[90000000];8 Charch[ the];9 intMain () {Tenscanf"%d",&n); One      for(intI=1; i<=n;i++){ Ascanf"%s",&ch); -L=strlen (CH); hash=1; -          for(intj=0; j<l;j++){ theHash= (hash*ch[j]*2351)%MoD; -         } -         if(!V[hash]) { -v[hash]=1; +++ans; -         } +     } Aprintf"%d\n", ans); at     return 0; -}

Results:

#1 AC2ms/102238kb #2ac3ms/102238kb#3ac2ms/19226kb#4AC 16ms/102238kb

#5 AC17ms/83347kb#6ac17ms/83316kb#7ac13ms/102238kb

#8 WA122ms/102238kb//the wrong answer. Score 0 on Line 1 column 3, read, expected.

#9 WA122ms/102238kb//the wrong answer. Score 0 on Line 1 column 4, read 1, expected 2.

#10 AC122ms/102238kb

Use a slightly more serious hash of the line.

Code implementation:

1#include <cstdio>2#include <cstring>3 using namespacestd;4 Const intMod=29989;5UnsignedintHash;6 intn,l,a,b,ans,v[30000];7 intid[10010][1510];8 Charch[1510];9 BOOLbjintXinty) {Ten     if(id[x][0]!=id[y][0])return 0; One      for(inti=id[x][0];i>id[y][0]-5; i--//Comparison of five, more time, well, actually compared to the last one after the comparison of length and random one (slightly back) should be on the line.  A     if(Id[x][i]!=id[y][i])return 0; -     return 1; - } the intMain () { -scanf"%d",&n); -      for(intI=1; i<=n;i++){ -scanf"%s",&ch); +id[i][0]=strlen (CH); hash=1; -          for(intj=0; j<id[i][0];j++){ +Hash= (hash*ch[j]* -)%MoD; Aid[i][j+1]=hash;//The middle value of the hash to save a bit, in fact, there is no need to save so much.  at         } -          while(V[hash]) {//If the hash value is the same, see if the string is the same.  -             if(BJ (V[hash],i)) Break; -hash++; -         } -         if(!V[hash]) {//If the string does not appear, drop it.  inv[hash]=i; -++ans; to         } +     } -printf"%d\n", ans); the     return 0; *}

A hash template problem, of course, is simpler with set or map.

"Template" string hash

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.