結果

問題 No.430 文字列検索
ユーザー moyashi_senpaimoyashi_senpai
提出日時 2016-10-03 00:22:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,944 ms / 2,000 ms
コード長 1,488 bytes
コンパイル時間 708 ms
コンパイル使用メモリ 87,616 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2024-05-03 04:28:09
合計ジャッジ時間 14,526 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,016 KB
testcase_01 AC 620 ms
6,144 KB
testcase_02 AC 749 ms
6,016 KB
testcase_03 AC 568 ms
6,016 KB
testcase_04 AC 4 ms
6,016 KB
testcase_05 AC 4 ms
6,016 KB
testcase_06 AC 3 ms
6,144 KB
testcase_07 AC 4 ms
6,016 KB
testcase_08 AC 5 ms
5,888 KB
testcase_09 AC 3 ms
6,144 KB
testcase_10 AC 5 ms
5,888 KB
testcase_11 AC 1,939 ms
6,144 KB
testcase_12 AC 1,944 ms
5,888 KB
testcase_13 AC 1,932 ms
6,016 KB
testcase_14 AC 1,898 ms
6,016 KB
testcase_15 AC 1,622 ms
6,016 KB
testcase_16 AC 840 ms
6,144 KB
testcase_17 AC 760 ms
6,016 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:72:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   72 |         scanf("%s", str);
      |         ~~~~~^~~~~~~~~~~
main.cpp:73:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   73 |         scanf("%d", &m);
      |         ~~~~~^~~~~~~~~~
main.cpp:77:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   77 |                 scanf("%s", pat);
      |                 ~~~~~^~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <bitset>
#include <stack>
#include <cstdlib>
#include <complex>
#include <unordered_map>
#define REP(i,n) for(int i = 0; n > i; i++)
#define MODU 33
#define Range(x,a,b) ((a) <= (x) && (x) <= (b))
#define POWT(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define C_D(c) ((c) - '0')
#define D_C(d) ((d) + '0')
using namespace std;
typedef vector<int> Ivec;
typedef pair<int, int> pii;
typedef long long int ll;
#include <cstdio>

void kmp_maketable(const char *key, int *table)
{
	table[0] = -1; table[1] = 0;
	int i = 2, j = 0;
	while (i == 0 || key[i - 1]) {
		if (key[i - 1] == key[j]) {
			table[i++] = ++j;
		}
		else if (j>0) {
			j = table[j];
		}
		else {
			table[i++] = 0;
		}
	}
}
int kmp_search(const char *str, const char *key, const int *table)
{
	int cou = 0;
	int i = 0, j = 0;
	while (str[i]) {
		if (str[i] == key[j]) {
			if (!key[j + 1]) {
				cou++;
			}
			i++, ++j;
		}
		else if (j>0) {
			j = table[j];
		}
		else {
			i++;
		}
	}
	return cou;
}

int main() {
	char str [500001];
	int m;
	scanf("%s", str);
	scanf("%d", &m);
	ll cou = 0;
	REP(i, m) {
		char pat[101];
		scanf("%s", pat);
		int table[500001];
		kmp_maketable(pat,table);
		cou += kmp_search(str,pat,table);
	}
	printf("%lld\n",cou);
	return 0;
}
0