#include #include #include #include #include using namespace std; typedef long long ll; #define Try 3 ll modpow(ll a, ll b, ll mod) { ll ret = 1, kakeru = a; while (b) { if (b & 1)ret *= kakeru, ret %= mod; kakeru *= kakeru, kakeru %= mod; b >>= 1; } return ret; } struct Rolling_Hash { const ll MD[Try] = { 1000000007, 999999937, 943759273 }; const ll base[Try] = { 109, 313, 1289 }; string X;//Xの連続部分列の中にYがあるかをチェック vector memo[Try]; int XL; void setup(const string& a = "") { X = a; for (int t = 0; t < Try; t++) { memo[t].resize(X.size() + 1); XL = X.size(); //(0, i]のハッシュ値の計算をする 累積和的にやる ll powbase = 1; memo[t][1] = X[0]; for (int i = 2; i <= XL; i++) { powbase *= base[t], powbase %= MD[t]; memo[t][i] = (memo[t][i - 1] + powbase * X[i - 1]) % MD[t]; } } } ll hash(int l, int r, int p = 0) { //[l, r) pはどの基底とMODでやるか if (r <= l || r > XL)return -1; ll ret = 0; ret = (memo[p][r] - memo[p][l] + MD[p]) % MD[p]; ret = (ret * modpow(modpow(base[p], l, MD[p]), MD[p] - 2, MD[p])) % MD[p]; return ret; } bool match(const string& Y) { int YL = Y.size(); for (int t = 0; t < Try; t++) { ll Yh = 0; ll powb = 1; for (int i = 0; i < YL; i++) { Yh = (Yh + Y[i] * powb) % MD[t]; powb *= base[t], powb %= MD[t]; } for (int i = 0; i + YL <= XL; i++) { ll Xh = (memo[t][i + YL] - memo[t][i] + MD[t]) % MD[t]; if (Xh == Yh)return true; Yh = (Yh * base[t]) % MD[t]; } } return false; } }; ll hash_from_str(const string& X, ll base, ll mod) { ll ret = 0, b = 1; for (int i = 0; i < X.size(); i++) { ret = (ret + X[i] * b) % mod; b = (b * base) % mod; } return ret; } int main() { string S; int M; cin >> S >> M; Rolling_Hash rh; rh.setup(S); map Map[10][Try]; for (int t = 0; t < Try; t++) { for (int i = 0; i < S.size(); i++) { for (int j = i + 1; j <= S.size() && j - i <= 10; j++) { Map[j - i - 1][t][rh.hash(i, j, t)]++; } } } ll ans = 0; while (M--) { string C; cin >> C; ll tmp = 1145141919810; for (int t = 0; t < Try; t++) { tmp = min(tmp, Map[C.size() - 1][t][hash_from_str(C, rh.base[t], rh.MD[t])]); } ans += tmp; } cout << ans << endl; return 0; }