HDOJ--4821 -- String [string hash], hdoj -- 4821 -- String

Source: Internet
Author: User

HDOJ--4821 -- String [string hash], hdoj -- 4821 -- String

Link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 4821

Question: For a string, select m substrings with the length of l to form a new string. The m substrings must be different from each other and ask how many combinations are there.


The string hash question has never been done before. Before doing this, bkdrhash has been used for two simple questions, POJ1200 and HDU1800.

The base Array records several seed values. base [I] indicates seed ^ I. This array is used to calculate the hash value of the sub-string. Preprocessing saves time.

If the string is previously hash, hash [I]-hash [I-l] * base [l] is the hash value of the substring [I-l, I, hash [I]-hash [I + l] * base [l] is the hash value of the substring [I, I + l.

Derivation process: take hash as an example. Assume that the string is abab and the length of the substring is 2.

I = 4 hash value (0 + a) * seed + B) * seed + a) * seed + B ),

Hash Value of I-l = 2 (0 + a) * seed + B), after base [2] (0 + a) * seed + B) * seed)

The two are subtracted to a * seed + B, which is the hash value of the AB corresponding to the interval [I-l, I.

Then, enumerate m l-length substrings according to each point. When their hash values are different, it is a result.


#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 100010#define eps 1e-7#define INF 0x7FFFFFFF#define seed 131typedef long long ll;typedef unsigned long long ull;char s[MAXN];ull base[MAXN],Hash[MAXN];map<ull,int> mp;int main(){    int m,l,i,len,ans;    base[0] = 1;    for(i=1;i<MAXN;i++) base[i] = base[i-1] * seed;    while(scanf("%d%d",&m,&l)!=EOF){        scanf("%s",s);        ans = 0;        len = strlen(s);        Hash[len] = 0;        for(i=len-1;i>=0;i--){            Hash[i] = Hash[i+1] * seed + s[i] - 'a';        }        for(i=0;i<l&&i+m*l<len;i++){            mp.clear();            for(int j=i;j<i+m*l;j+=l){                ull temp = Hash[j] - Hash[j+l] * base[l];                mp[temp]++;            }            if(mp.size()==m)    ans++;            for(int j=i+m*l;j+l<=len;j+=l){                ull temp = Hash[j-m*l] - Hash[j-(m-1)*l] * base[l];                mp[temp]--;                if(!mp[temp])   mp.erase(temp);                temp = Hash[j] - Hash[j+l] * base[l];                mp[temp]++;                if(mp.size()==m)    ans++;            }        }        printf("%d\n",ans);    }    return 0;}



What is the problem solved by the string hash function?

What is HASH in eMule?

Many old Yu's colleagues may not be clear about this issue as I did for new users. I have reviewed some information and explained it to you based on the features of eMule.

First, we often talk about such words as Hash, UserHash, and file Hash.

In fact, Hash is translated into Chinese, and it is also called a Hash function in programming.

So what role does this function play in the P2P software of eMule?

Here we need to understand a concept of MD (MD2, MD4, MD5)

We all know that movies, music, software, and so on are stored on computing machines as files.

But sometimes we do not need to see all the files, or part of them to understand this file.

Just like reading a book, you only need to know the contents of the entire book as long as you know the directory.

It was developed by MIT Laboratory for Computer Science and Ronald L. Rivest of RSA Data Security Inc in early 1990s.

Generates a set of Message-Digest Algorithm <MD> (Information-Digest Algorithm;

At the same time, we introduced the HASH function (HASH)

The HASH function provides a calculation process: enter a character string with an unfixed length, and return a string with a fixed length, also known as the HASH value.

The unidirectional HASH function is used to generate information summaries.

When we put a file into the shared file of eMule

We started this algorithm step (everyone can realize that hard disk conversion-extract File Information)

Finally, through this series of algorithms, we get a 128 binary bit.

Ps: the hash algorithm is used to verify the integrity of files.

When the first person converts his/her shared file into a HASH value

At the same time, there may be many contributors, so their HASH values will enter a dynamic list of servers.

The list stores the IP address, PORT, and other address information of users with the same files.

When another user needs to download or search, the server sends this information to the user.

So this user knows where to download it.

After the protocol verification and other program processes are completed, two small zookeeper nodes start point-to-point propagation.

In JAVA, what is the role of the hashCode () method in the String class?

Hash should be a comparison of implementation values.
The hashCode value is not necessarily related to object reference. In java, two strings with the same value are not necessarily the same object. For example, the and B above are two different objects.
The hash value of a string is calculated based on the string value. The hash value of a string object with the same value must be the same. The specific calculation method is described in the jdk documentation.
Each java object has a unique identifier. The hash method in the object class directly returns the internal ID of the object, which is different from the string hash method, the object hash method can be used to distinguish different objects. because the original object does not have any meaningful value, it can be used to calculate the 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.