結果

問題 No.430 文字列検索
ユーザー kkktymkkktym
提出日時 2019-09-10 00:02:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,795 ms / 2,000 ms
コード長 1,412 bytes
コンパイル時間 1,183 ms
コンパイル使用メモリ 67,160 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-15 12:56:24
合計ジャッジ時間 14,732 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 601 ms
4,380 KB
testcase_02 AC 942 ms
4,376 KB
testcase_03 AC 577 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 1,744 ms
4,376 KB
testcase_12 AC 1,743 ms
4,376 KB
testcase_13 AC 1,736 ms
4,376 KB
testcase_14 AC 1,795 ms
4,380 KB
testcase_15 AC 1,603 ms
4,376 KB
testcase_16 AC 997 ms
4,380 KB
testcase_17 AC 948 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
using namespace std;
struct KMP{
  string s;
  int n;
  vector<int> table;
  
  KMP(string _s)
  {
    s = _s;
    n = s.size();
  }
  //kmpテーブルの作成
  void make_kmp_table(string p)
  {
    int ps = p.size();
    table.resize(ps+1);
    table[0] = -1;
    int j = -1;
    rep(i,ps){
      while(j>=0&&p[i]!=p[j]) j = table[j];
      j++;
      table[i+1] = j;
    }
  }
  //kmp法
  int kmp_search(string p)
  {
    int res;
    make_kmp_table(p);
    int ps = p.size();
    bool f = false;
    int i = 0,j = 0;
    while(i<n){
      while(s[i]==p[j]){
	i++;j++;
	if(j==ps){
	  f = true;
	  res = i-j;
	  break;
	}
	if(i==n) break;      
      }
      if(j==0) i++;
      else j = table[j];
    }
    return f?res:-1;
  }

  int kmp_count(string p)
  {
    int res = 0;
    make_kmp_table(p);
    int ps = p.size();
    int i = 0,j = 0;
    while(i<n){
      while(s[i]==p[j]){
	i++;j++;
	if(j==ps){
	  res++;
	}
	if(i==n) break;      
      }
      if(j==0) i++;
      else j = table[j];
    }
    return res;
  }

};
int main()
{
  string s;
  cin >> s;
  KMP kmp(s);

  int m;
  cin >> m;

  vector<string> c(m);
  rep(i,m) cin >> c[i];

  int res = 0;
  rep(i,m){
    res += kmp.kmp_count(c[i]);
  }

  cout << res << "\n";
  
  return 0;
}
0