ICPC Notebook

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub tatyam-prime/ICPC_notebook

:heavy_check_mark: test/string/KMP.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_14_B"
#include "test/template.hpp"
#include "src/string/KMP.hpp"

int main() {
   cin.tie(0)->sync_with_stdio(0);
   string S, T;
   cin >> S >> T;
   auto s = T + '$' + S;
   auto kmp = KMP(s);

   rep(i, sz(s) - sz(S), sz(s)) {
      if(kmp[i] == sz(T)) cout << i - sz(T) * 2 << '\n';
   }
}
#line 1 "test/string/KMP.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_14_B"
#line 1 "test/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = LLONG_MAX / 4;
#define rep(i, a, b) for(ll i = a; i < (b); i++)
#define all(a) begin(a), end(a)
#define sz(a) ssize(a)
bool chmin(auto& a, auto b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto& a, auto b) { return a < b ? a = b, 1 : 0; }
#line 1 "src/string/KMP.hpp"
// kmp[i] := max{ l ≤ i | s[:l] == s[(i+1)-l:i+1] }
// abacaba -> 0010123
auto KMP(string s) {
   vector<ll> p(sz(s));
   rep(i, 1, sz(s)) {
      ll g = p[i - 1];
      while(g && s[i] != s[g]) g = p[g - 1];
      p[i] = g + (s[i] == s[g]);
   }
   return p;
}
#line 4 "test/string/KMP.test.cpp"

int main() {
   cin.tie(0)->sync_with_stdio(0);
   string S, T;
   cin >> S >> T;
   auto s = T + '$' + S;
   auto kmp = KMP(s);

   rep(i, sz(s) - sz(S), sz(s)) {
      if(kmp[i] == sz(T)) cout << i - sz(T) * 2 << '\n';
   }
}
Back to top page