結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  tancahn2380 | 
| 提出日時 | 2018-07-25 19:28:37 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 444 ms / 2,000 ms | 
| コード長 | 3,219 bytes | 
| コンパイル時間 | 735 ms | 
| コンパイル使用メモリ | 106,692 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-11-10 00:19:38 | 
| 合計ジャッジ時間 | 5,712 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 14 | 
ソースコード
# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <tuple>
# include <unordered_map>
# include <numeric>
# include <complex>
# include <bitset>
# include <random>
# include <chrono>
# include <cstdlib>
# include <tuple>
# include <array>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using D = double;
constexpr int INF = 2147483647;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr double HDINF = 50000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define RALL(qpqpq)           (qpqpq).rbegin(),(qpqpq).rend()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define RFOR(i,tptpt,ypypy)  for(LL i=(tptpt);i>=(ypypy);i--)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)
const ULL B[] = { 999999937ULL,1000000007ULL };
//aはbにいくつ含まれているか?
int contain(string a, string b) {
	
	int ret = 0;
	int al = a.length(), bl = b.length();
	if (al > bl)return false;
	//Bのal乗を計算
	ULL t[] = { 1 ,1 };
	for (int i = 0; i < al; i++)t[0] *= B[0];
	for (int i = 0; i < al; i++)t[1] *= B[1];
	//aとbの最初のal文字に関するハッシュ値を計算
	ULL ah[] = { 0 ,0 }, bh[] = { 0 ,0 };
	for (int i = 0; i < al; i++)ah[0] = ah[0] * B[0] + a[i];
	for (int i = 0; i < al; i++)bh[0] = bh[0] * B[0] + b[i];
	for (int i = 0; i < al; i++)ah[1] = ah[1] * B[1] + a[i];
	for (int i = 0; i < al; i++)bh[1] = bh[1] * B[1] + b[i];
	//bの場所を1つずつ進めながらハッシュ値をチェック
	for (int i = 0; i + al <= bl; i++) {
		if (ah[0] == bh[0] && ah[1] == bh[1])ret++;//bのi文字目からのal文字が一致
		if (i + al < bl) {
			bh[0] = bh[0] * B[0] + b[i + al] - b[i] * t[0];
			bh[1] = bh[1] * B[1] + b[i + al] - b[i] * t[1];
		}
	}
	//return false;//一致する文字列がない
	return ret;
}
string s;
string c[5050];
int m;
int main() {
	cin >> s >> m;
	int ret = 0;
	REP(i, m) {
		cin >> c[i];
		ret+= contain(c[i], s);
	}
	cout << ret << endl;
}
            
            
            
        