結果

問題 No.430 文字列検索
ユーザー mdj982mdj982
提出日時 2018-03-10 03:01:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,799 bytes
コンパイル時間 1,218 ms
コンパイル使用メモリ 129,460 KB
実行使用メモリ 8,320 KB
最終ジャッジ日時 2024-04-19 13:42:02
合計ジャッジ時間 14,975 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 TLE -
testcase_02 AC 1,144 ms
5,376 KB
testcase_03 AC 1,125 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 7 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 6 ms
5,376 KB
testcase_11 AC 1,559 ms
5,376 KB
testcase_12 AC 1,113 ms
5,376 KB
testcase_13 AC 1,097 ms
5,376 KB
testcase_14 AC 1,113 ms
5,376 KB
testcase_15 AC 1,116 ms
5,376 KB
testcase_16 AC 1,125 ms
5,376 KB
testcase_17 AC 1,144 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <climits>
#include <limits>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <unordered_map>
#include <random>
#define _USE_MATH_DEFINES
#include <cmath>
#include <complex>

using namespace std;

#define INFD numeric_limits<double>::infinity()
#define INFL (int)1e8
#define INFLL (long long)1e15
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
#define bitmanip(m,val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

/*******************************************************/

class Hash_String {
private:
  int kind;
  char base;
  ll powll(ll n, ll p) {
    if (p == 0) return 1;
    else if (p == 1) return n;
    else {
      ll ans = powll(n, p / 2);
      ans = ans * ans;
      if (p % 2 == 1) ans = ans * n;
      return ans;
    }
  }
  vll hash_vec_accumulate(string a, int length) {
    int n = a.length();
    if (n - length + 1 <= 0) return{};
    vll ret(n - length + 1, 0);
    if (n == 0) return ret;
    ret[0] = make_hash_of(a.substr(0, length));
    Loop1(i, ret.size() - 1) {
      ret[i] = ret[i - 1] / kind + powll(kind, length - 1) * (a[i + length - 1] - base);
    }
    return ret;
  }
public:
  // only for consecutive charactors in ASCII 
  Hash_String(int kind, char base) {
    Hash_String::kind = kind;
    Hash_String::base = base;
  }
  ll make_hash_of(string s) {
    ll ret = 0;
    ll x = 1;
    Loop(i, s.length()) {
      ret += (s[i] - base) * x;
      x *= kind;
    }
    return ret;
  }
  // return series of indexes
  vvi search_str(string source, vector<string> destination) {
    int n = source.length();
    map<int, vll> mp;
    vvi ret(destination.size());
    Loop(i, destination.size()) {
      int m = destination[i].length();
      if (mp.find(m) == mp.end()) {
        mp[m] = hash_vec_accumulate(source, m);
      }
      ll d_hash = make_hash_of(destination[i]);
      Loop(j, mp[m].size()) {
        if (d_hash == mp[m][j]) ret[i].push_back(j);
      }
    }
    return ret;
  }
};

int main() {
  string s; cin >> s;
  int q; cin >> q;
  vector<string> ts(q);
  Loop(i, q) {
    cin >> ts[i];
  }
  Hash_String hs(26, 'A');
  vvi result = hs.search_str(s, ts);
  int ans = 0;
  Loop(i, result.size()) {
    ans += result[i].size();
  }
  cout << ans << endl;
}
0