結果
| 問題 |
No.1005 BOT対策
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-03-07 13:50:40 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 3,879 bytes |
| コンパイル時間 | 1,005 ms |
| コンパイル使用メモリ | 116,032 KB |
| 実行使用メモリ | 7,848 KB |
| 最終ジャッジ日時 | 2025-06-20 00:50:18 |
| 合計ジャッジ時間 | 1,959 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
/**
* author: otera
**/
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x) cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ローリングハッシュ
// 二分探索で LCP を求める機能つき
struct RollingHash {
static const int base1 = 1007, base2 = 2009;
static const int mod1 = 1000000007, mod2 = 1000000009;
vector<long long> hash1, hash2, power1, power2;
// construct
RollingHash(const string &S) {
int n = (int)S.size();
hash1.assign(n+1, 0);
hash2.assign(n+1, 0);
power1.assign(n+1, 1);
power2.assign(n+1, 1);
for (int i = 0; i < n; ++i) {
hash1[i+1] = (hash1[i] * base1 + S[i]) % mod1;
hash2[i+1] = (hash2[i] * base2 + S[i]) % mod2;
power1[i+1] = (power1[i] * base1) % mod1;
power2[i+1] = (power2[i] * base2) % mod2;
}
}
// get hash of S[left:right)
inline pair<long long, long long> get(int l, int r) const {
long long res1 = hash1[r] - hash1[l] * power1[r-l] % mod1;
if (res1 < 0) res1 += mod1;
long long res2 = hash2[r] - hash2[l] * power2[r-l] % mod2;
if (res2 < 0) res2 += mod2;
return {res1, res2};
}
// get lcp of S[a:] and S[b:]
inline int getLCP(int a, int b) const {
int len = min((int)hash1.size()-a, (int)hash1.size()-b);
int low = 0, high = len;
while (high - low > 1) {
int mid = (low + high) >> 1;
if (get(a, a+mid) != get(b, b+mid)) high = mid;
else low = mid;
}
return low;
}
// get lcp of S[a:] and T[b:]
inline int getLCP(const RollingHash &T, int a, int b) const {
int len = min((int)hash1.size()-a, (int)hash1.size()-b);
int low = 0, high = len;
while (high - low > 1) {
int mid = (low + high) >> 1;
if (get(a, a+mid) != T.get(b, b+mid)) high = mid;
else low = mid;
}
return low;
}
};
void solve() {
string s, t; cin >> s >> t;
int n = (int)s.size(), m = (int)t.size();
if(n < m) {
cout << 0 << endl;
return;
}
RollingHash rhs(s), rht(t);
int i = 0, ans = 0;
for(; i <= n - m;) {
if(rhs.get(i, i + m) == rht.get(0, m)) {
if(m == 1) {
cout << -1 << endl;
return;
}
++ ans;
i += m - 1;
} else {
++ i;
}
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(10);
//int t; cin >> t; rep(i, t)solve();
solve();
return 0;
}