"Two-point answer + mentally retarded string hash" bzoj2946-[poi2000] common string (ranklist inverted one reached!!!!!) "Knowledge points with hash"

Source: Internet
Author: User
Tags mul

"The main topic"

Give a few words made up of lowercase letters to find the length of their longest common substring.

"Small notes of string hash"

hash[i]= (Hash[i-1]*p+idx (S[i]))%mod, IDX is a mapped value, typically a. Z-MAP 1: 26;

In practice, p takes a 6 to 8-bit prime number, and the MoD generally takes a large prime number 1e9+7 (1000000007) or 1e9+9 (1000000009).

Hash[i]= (Hash[i-1]*p+idx (S[i]))%mod denotes the hash value of the prefix of I, is a hash prefix and, then, requires S[L...R] hash value of this substring:
hash[l. R]= (hash[r]-hash[l-1]* (p^ (r-l+1)))%mod(assuming string subscript starting from 1)

Ideas

is said to be the suffix automaton, however Konjac Konjac does not have the suffix automaton, so the brace-up uses the hash to make a mess. The last time a flag, what the ugliest code of the year, this is it ... The product of brain cavity is not the positive solution, can see the RP completely.

First pudgy the hash value of each word, and then happily start the length of the two common substrings.

The two arrays of the VIS and Times are set in a shaggy manner. Times[i] indicates that the current hash value I has appeared in several words. Enumerate the starting position of the substring of each word, O (1) Time to hash the substring, then the Times plus one. Of course, the same long word may have the same substring, so use Vis to record this hash value finally appears in the first few words, to determine whether the substring in the current string is not the initial occurrence. If the times are added to N, it means that each word has this substring, the solution is correct, oh yes! Time complexity, the hash is O (| S|*n), | The s| represents the longest substring length. The two-part answer is (log| s|*n*| s|). So the overall expectation complexity is O (log| s|*| S|*n), and N only 5,| S| is only 2000, apparently easy to pass. Well, that's it, end it.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6 #defineMoD 100000197 #defineP 200118 using namespacestd;9typedefLong Longll;Ten Const intmaxn=Ten; One Const intmaxlen= -+ -; A intN,minl; - ll Hash[maxn][maxlen]; - CharStr[maxlen]; the intLEN[MAXN]; - intVis[mod+1],times[mod+1]; -  - intCheckintx) + {  -memset (Vis,0,sizeof(Vis)); +Memset (Times,0,sizeof(Times)); All mul=1; at      for(intI=1; i<=x;i++) mul= (mul*p)%MoD; -      for(intI=1; i<=n;i++) -          for(intj=x;j<=len[i];j++) -         { -ll nowhash= (hash[i][j]-(hash[i][j-x]*mul)%mod+mod)%MoD; -             if(vis[nowhash]!=i) in             { -times[nowhash]++; tovis[nowhash]=i; +                 if(times[nowhash]==n)return 1; -             } the         } *     return 0; $ }Panax Notoginseng  - voidInit () the { +Minl=0x7fffffff; Ascanf"%d",&n); the      for(intI=1; i<=n;i++) +     { -scanf"%s", str); $len[i]=strlen (str); $         if(LEN[I]&LT;MINL) minl=Len[i]; -hash[i][1]=str[0]-'a'+1; -          the          for(intj=2; j<=len[i];j++) -Hash[i][j]= (hash[i][j-1]*p+ (str[j-1]-'a'+1))%MoD;Wuyi     }         the } -  Wu voidSearch_ans () - { About     intlb=0, ub=minl+1; $      while(lb<UB) -     { -         intMid= (Lb+ub) >>1; -         if(Check (mid)) lb=mid+1;Elseub=mid; A     } +printf"%d", lb-1); the } -  $ intMain () the { the init (); the Search_ans (); the     return 0; -}

"Two-point answer + mentally retarded string hash" bzoj2946-[poi2000] common string (ranklist inverted one reached!!!!!) "Knowledge points with 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.