Skip to content

Commit

Permalink
HDU7433
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-yidie committed Oct 31, 2024
1 parent 3a3ef20 commit 9fff3b5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions HDU7433.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include<bits/stdc++.h>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL;
typedef pair<int,int> pii;
const int N = 1e6+100000 , mod = 1e9+7 , P = 131;

ULL h[N] , p[N] , h1[N];

void init() {
p[0] = 1;
for (int i = 1; i < N ; i++) {
p[i] = p[i-1] * P ;
}
}

ULL get( int l , int r ) {
return h[r] - h[l-1] *p[r-l+1];
}

ULL get1(int l , int r) {
return h1[r]-h1[l-1]*p[r-l+1];
}

void AC() {
string a , b ; cin >> a >> b ;
int lena = a.size() , lenb = b.size() ;
a = " " + a + a;
b = " " + b ;
map<ULL , int> mp;

for (int i = 1 ; i <= lena*2; i++ ){
h[i] = h[i-1] * P + a[i];
}
for (int i = 1; i+lena-1 < a.size(); i++) {
mp[ get(i,i+lena-1) ] = 1;
}
for (int i = 1 ; i <= lenb ; i++ ) {
h1[i] = h1[i-1]*P + b[i];
}
LL cnt = 0 ;
for (int i = 1 ; i + lena - 1 <= lenb ; i++ ) {
if (mp[ get1(i,i+lena-1) ]) cnt++;
}
cout << cnt << '\n';
}

int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int T = 1 ;
init();
cin >> T ;
while (T--) {
AC();
}
return 0;
}
Binary file added HDU7433.exe
Binary file not shown.
Binary file modified output/CF1924A.exe
Binary file not shown.
Binary file added output/HDU7433.exe
Binary file not shown.

0 comments on commit 9fff3b5

Please sign in to comment.