Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

HASH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Posted by rydrydryd at 2012-01-17 11:39:42 on Problem 3461 and last updated at 2012-01-17 11:41:53
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define mo 199997
#define cp freopen
#define M(x) x%=mo
#define MAX 1000010
#define rep(i,a,b) for (int i=a;i<=b;i++)
using namespace std;
int n; 
char s1[MAX],s2[MAX]; 

struct hash_table
{
      int s,k,l;
      int e[10010]; 
      void update(int x,int y)
      {
            s-=x*e[l+1],s*=k; 
            s=(s%mo+mo)%mo;
            s+=e[2]*y,M(s); 
      }
      void pre()
      {
            e[0]=1; 
            rep(i,1,10005)
                  e[i]=e[i-1]*k,M(e[i]); 
      }
      void calc(int L,char *S)
      {
            s=0; 
            l=L; 
            rep(i,0,l-1)
                  s+=e[l-i+1]*S[i],M(s); 
      }
}a; 

void solve()
{
      gets(s1);
      gets(s2);
      int l1=strlen(s1); 
      a.calc(l1,s1);
      int a1=a.s;
      int l2=strlen(s2);
      a.calc(l1,s2);
      int ans=a1==a.s;
      rep(i,1,l2-l1)
      {
            a.update(s2[i-1],s2[l1-1+i]);
            ans+=a1==a.s;
      }
      cout<<ans<<endl; 
}

int main()
{
      a.k=47; 
      a.pre(); 
      cin>>n; 
      gets(s1); 
      rep(i,1,n)
            solve(); 
      
      return 0;   
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator