結果

問題 No.430 文字列検索
ユーザー 沙耶花沙耶花
提出日時 2020-07-12 14:51:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,151 bytes
コンパイル時間 2,261 ms
コンパイル使用メモリ 205,976 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 03:27:18
合計ジャッジ時間 3,122 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 17 ms
6,944 KB
testcase_02 AC 13 ms
6,940 KB
testcase_03 AC 11 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 10 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 23 ms
6,940 KB
testcase_12 AC 23 ms
6,940 KB
testcase_13 AC 23 ms
6,940 KB
testcase_14 AC 18 ms
6,944 KB
testcase_15 AC 15 ms
6,940 KB
testcase_16 AC 14 ms
6,940 KB
testcase_17 AC 14 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000

struct trie{
	struct node{
		vector<int> next;
		int str=-1;
		node(int k){
			next.resize(k,-1);
		}
	};
	vector<node> _N;
	vector<string> _S;
	int n;
	trie(int k){
		n = k;
		_N.push_back(node(n));
	}
	
	void add(string s){
		int now = 0;
		for(int i=0;i<s.size();i++){
			int c = get(s[i]);
			if(_N[now].next[c]==-1){
				_N[now].next[c] = _N.size();
				_N.push_back(node(n));
			}
			now = _N[now].next[c];
		}
		
		_N[now].str = _S.size();
		_S.push_back(s);
	}
	
	int check(string s){
		int now = 0;
		for(int i=0;i<s.size();i++){
			int c = get(s[i]);
			if(_N[now].next[c]==-1)return -1;
			now = _N[now].next[c];
		}
		return _N[now].str;
	}
	
	int get(char c){
		return c-'A';
	}
	
};

int main(){
	
	string S;
	cin>>S;
	int M;
	cin>>M;
	
	trie T(26);
	for(int i=0;i<M;i++){
		string s;
		cin>>s;
		T.add(s);
	}
	
	int ans = 0;
	for(int i=1;i<=10;i++){
		for(int j=0;j+i<=S.size();j++){
			int ret = T.check(S.substr(j,i));
			if(ret!=-1)ans++;
		}
	}
	
	cout<<ans<<endl;
	
	return 0;
}
0