結果

問題 No.430 文字列検索
ユーザー furuya1223furuya1223
提出日時 2017-08-25 14:53:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,643 bytes
コンパイル時間 1,853 ms
コンパイル使用メモリ 168,236 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-23 15:19:00
合計ジャッジ時間 17,799 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 979 ms
6,816 KB
testcase_02 AC 871 ms
6,940 KB
testcase_03 AC 807 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 6 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 4 ms
6,944 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 1,857 ms
6,944 KB
testcase_16 AC 988 ms
6,940 KB
testcase_17 AC 894 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#include <sys/timeb.h>
#include <fstream>
 
using namespace std;
 
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define replrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) replrev(i,0,n)
#define repi(itr,ds) for(auto itr=ds.begin();itr!=ds.end();itr++)
#define all(a) a.begin(),a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 1000000000000000000LL
#define EPS (1e-10)
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295
 
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int> > vvi;
typedef vector<vector<bool> > vvb;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<double> > vvd;
typedef vector<vector<P> > vvP;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqlll;
typedef priority_queue<P, vector<P>, greater<P> > pqlP;
/*
// シンプルな構文解析用
typedef string::const_iterator State;
class ParseError {};
//*/
struct Edge {
	int from, to, cost;
	bool operator<(Edge e) {
		return cost < e.cost;
	}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

vector<int> makeTableKMP(const string &str){
	vector<int> border(str.size() + 1);
	border[0] = -1;
	int j = -1;
	for (int i = 0; i < str.size(); i++) {
		while (j >= 0 && str[i] != str[j]) j = border[j];
		j++;
		//border[i+1] = j;
		if (str[i+1] == str[j]) border[i+1] = border[j]; 
		else border[i+1] = j;
	}
	return border;
}

vector<int> searchKMP(const string& str, const string& word) {
	vector<int> table = makeTableKMP(word), ret;
	int m = 0, i = 0, n = str.size();
/*	while (m + i < n) {
cout<<m<<" "<<i<<endl;
		if (word[i] == str[m + i]) {
			if (++i == (int)(word.size())) {
				ret.push_back(m);
				m = m + i - table[i];
				i = table[i];
			}
		} else {
			m = m + i - table[i];
			if (i > 0) i = table[i];
		}
	}
*/
int j;
i= j = 0;
   while (j < n) {
      while (i > -1 && word[i] != str[j]) i = table[i];
      i++;
      j++;
      if (i >= word.size()) {
         ret.push_back(j - i);
         i = table[i];
      }
   }
	return ret;
}

int main(void) {

	cin.tie(0);

	ios::sync_with_stdio(false);
	string S;
	int M;
	cin >> S >> M;
	ll ans = 0;
	rep(i, M){
		string word;
		cin >> word;
		ans += searchKMP(S, word).size();
	}
	cout << ans << endl;
	return 0;
}
0