BZOJ 3555 CTSC 2014 Penguin QQ Hash

Source: Internet
Author: User

To distinguish the qq accounts from the accounts of a person, we need to write a program to determine which names are similar. Similar definitions mean that there are different characters with only one position.


Idea: The data range is not too large. It is obvious that Hash is not used for binary. I heard from my teacher that one of the students who took the CTSC test this year did not answer this question because they thought too much. He wrote the Hash at the time, and then he had nothing to do. He had a set of data cards and lost his hash, and then he did not dare to hand in the hash. Finally, he handed in a Trie tree and the result was T .. The lessons of blood tell us not to think too much ..

Preprocessing each string first to obtain the hash value. Then enumerate each bit. In the O (1) time, obtain the Hash removed from this bit, and then judge the weight. I wanted to hand-write the hash table. After reading the online question solution, I found that the problem could be solved quickly...


CODE:

# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # define MAX 30010 # define BASE 137 using namespace std; int points, length; char src [MAX] [210]; unsigned long hash [MAX] [210], power [MAX]; unsigned long _ hash [MAX]; void Pretreatment () {power [0] = 1; for (int I = 1; I <= length; ++ I) power [I] = power [I-1] * BASE ;} inline void GetHash (char * s, unsigned long hash []) {for (int I = 1; I <= length; ++ I) hash [I] = hash [I-1] * BASE + s [I];} inline unsigned long CalcHash (unsigned long hash [], int I) {unsigned long re = hash [length]; re = re-hash [I] * power [length-I] + hash [I-1] * power [length-I + 1]; return re;} int main () {scanf ("% d % * d", & points, & length); Pretreatment (); for (int I = 1; I <= points; ++ I) {scanf ("% s", src [I] + 1); GetHash (src [I], hash [I]);} int ans = 0; for (int I = 1; I <= length; ++ I) {for (int j = 1; j <= points; ++ j) _ hash [j] = CalcHash (hash [j], I); sort (_ hash + 1, _ hash + points + 1 ); _ hash [points + 1] = 0; int start = 1; for (int j = 1; j <= points + 1; ++ j) if (_ hash [j]! = _ Hash [start]) {int cnt = j-start; ans + = cnt * (cnt-1)> 1; start = j ;}} cout <ans <endl; return 0 ;}


BZOJ 3555 CTSC 2014 Penguin QQ 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.