Word search


But small can the school library administrator, and now he inherited a very difficult task. Since the schools need some materials, the principal need in the article inspection
search some information. Chancellor total cocoa N to a small article, each article as a string. Now, the president needs him to find such a word, it is at least
M N articles in this article appeared in the article, and word length L. However, the workload is very large, but the president has little cocoa urgent need to complete this task
. Now he turned to you, you need to write a program to complete this arduous task
Input
row 13 positive integers N, M, L, represents the number of articles, the word appears at least in length and M articles in each word.
Next N lines of a string that represents the article.
1≤N, M≤2000, L≤1000. The length of each article is no greater than 1000, are lowercase letters
Output
only one line, the number of words expressed satisfying the search condition.

The Input the Sample
3 2 2
NOIP
istudycpp
imacppstudent
the Sample the Output
5
// These five words are: st, tu, ud, pp , cp.

 

#include <the iostream> 
#include <stdio.h> 
#include <string.h> 
the using namespace STD; 
typedef Long Long LL; 
const int MaxN = 2005; 
const int MaxM = 1000005; 
const int MINMOD = 999 973; 
const = LL MaxMod 499999999999993ll; 
struct the Node 
{ 
 int NO, cnt; // NO is where the word article number, cnt is the number of occurrences of the word 
 LL s; // s is a word hashh value 
 int next; // resolve the conflict with the open hash 
} h [ MaxM]; 
int N, M, L, TOT, ANS; 
int first [MaxM]; 
char STR [MaxN] [MaxN]; 
void the Add (U int, int x, LL S) 
// in the article in the x short hashh value appears u, s word length hashh value added hashh table 
{ 
 ++ TOT; 
 H [TOT] .s = s; 
 H [TOT] = .cnt. 1; 
 H [TOT] .no = X; 
 h [tot] .next = first [ u];
 First [U] = TOT; 
} 
void hashh (X int, U int, LL S) 
{int I; 
 for (I = First [U]; I = 0;! I = H [I] .next) 
   IF (S == h [i] .s) // appear in hashh table over 
     { 
     IF (h [i] == x .no) return; 
     // x also appears in the first article, do not have control 
      h [i] X = .no; 
      H [I] .cnt ++; // number appears plus. 1 
      return; 
     } 
 the add (U, X, S); 
} 
void the init () 
{ 
    int I; 
    Scanf ( "% D% D% D" , & N, & M, & L); 
    for (I =. 1; I <= N; I ++) 
    Scanf ( "% S", & STR [I]); 
    ANS = 0; 
} 
void Work () 
{int I, J, U , K, K1; 
 LL S, K2; 
 K1 = K2 =. 1; 
 for (J = 0; J <-L. 1; J ++) 
 { 
    K1 = K1 * 26 is MINMOD%; // magnitude 
    k2 = k2 * 26% MaxMod;
 }
 for(i=1;i<=N;i++)
 {
    u=s=0;
    for (j=0;j<L;j++)
    {
       u=(u*26+str[i][j])%MinMod;//短hashh值 
       s=(s*26+str[i][j])%MaxMod;//长hashh值 
    }
    hashh(i,u,s);
    k=strlen(str[i]);
    for(j=L;j<k;j++)
      {
       u=((u-str[i][j-L]*k1)%MinMod+MinMod)%MinMod;//前面去一位后面加一位 
       s=((s-str[i][j-L]*k2)%MaxMod+MaxMod)%MaxMod;
       u=(u*26+str[i][j])%MinMod;
       s=(s*26+str[i][j])%MaxMod;
       hashh(i,u,s);
      }
   }
}
void output()
{
 int i;
 for(i=1;i<=tot;i++)
   if(h[i].cnt>=M) ++ans;
 printf("%d\n",ans);
}
int main(void)
{
 init();
 work();
 output();
 return 0;
}



#include<bits/stdc++.h>
#define ll long long
#define mod 28282789
#define L 1001
using namespace std;
inline void read( int &x){
     int datta=0; char chchc= getchar (); bool okoko=0;
     while (chchc< '0' ||chchc> '9' ){ if (chchc== '-' )okoko=1;chchc= getchar ();}
     while (chchc>= '0' &&chchc<= '9' ){datta=datta*10+chchc- '0' ;chchc= getchar ();}
     x=okoko?-datta:datta;
}
int n,m,l,len,times[mod+10],ans;
int clear[L],nc;
ll base=26,q[L],hx[L];
char c[L];
bool did[mod+10];
ll fk( int a, int b){ return (hx[b]-(hx[a-1]*q[b-a+1])%mod+mod)%mod;}
int main(){
     read(n),read(m),read(l);q[0]=1;
     for ( int i=1;i<=L;i++)q[i]=(q[i-1]*base)%mod;
     for ( int i=1;i<=n;i++){
         scanf ( "%s" ,c+1);
         len= strlen (c+1);
         for ( int i=1;i<=len;i++)hx[i]=(hx[i-1]*base+c[i]- 'a' +1)%mod;
         for ( int i=1;i+l-1<=len;i++){
             ll tp=fk(i,i+l-1);
             if (!did[tp]){
                 did[tp]= true ;clear[++nc]=tp;
                 if (++times[tp]==m)ans++;
             }
         }
         for ( int i=1;i<=nc;i++){did[clear[i]]= false ;}
         nc=0;
     }
     printf ( "%d\n" ,ans);
     return 0;
}

  

Guess you like

Origin www.cnblogs.com/cutemush/p/12297616.html