#include #include #define incID(i, l, r) for(int i = (l) ; i < (r); i++) #define incII(i, l, r) for(int i = (l) ; i <= (r); i++) #define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--) #define decII(i, l, r) for(int i = (r) ; i >= (l); i--) #define inc( i, n) incID(i, 0, n) #define inc1(i, n) incII(i, 1, n) #define dec( i, n) decID(i, 0, n) #define dec1(i, n) decII(i, 1, n) typedef long long signed int LL; typedef long long unsigned int LU; template void swap(T &x, T &y) { T t = x; x = y; y = t; return; } template T abs(T x) { return (0 <= x ? x : -x); } template T max(T a, T b) { return (b <= a ? a : b); } template T min(T a, T b) { return (a <= b ? a : b); } template bool setmin(T &a, T b) { if(a <= b) { return false; } else { a = b; return true; } } template bool setmax(T &a, T b) { if(b <= a) { return false; } else { a = b; return true; } } template T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); } template T lcm(T a, T b) { return a / gcd(a, b) * b; } // ---- ---- int m; char s[50001]; char c[5000][11]; char sc[50001]; int sn[50001]; char cc[5000][11]; int cn[5000][11]; int ans; void encode(char str[], char c_char[], int c_int[]) { int i = 0, p = -1; char pre = '@'; while(str[i] != '\0') { if(str[i] != pre) { c_char[++p] = str[i]; c_int[p] = 1; } else { c_int[p]++; } pre = str[i++]; } c_char[++p] = '\0'; c_int[p] = 1; return; } void test(char str[], char code_s[], int code_i[]) { //printf("%s\n", str); int i; i = 0; while(code_s[i] != '\0') { printf("%c", code_s[i]); i++; } printf("\n"); i = 0; while(code_s[i] != '\0') { printf("%d", code_i[i]); i++; } printf("\n"); printf("\n"); return; } int main() { scanf("%s%d", s, &m); inc(i, m) { scanf("%s", c[i]); } encode(s, sc, sn); inc(i, m) { encode(c[i], cc[i], cn[i]); } // test(s, sc, sn); // inc(i, m) { test(c[i], cc[i], cn[i]); } inc(i, m) { int j = 0; while(sc[j] != '\0') { int k = 0, x, y; bool flag = true; while(cc[i][k] != '\0') { // printf("j; %2d, k: %d, %c, %c\n", j, k, sc[j + k], cc[i][k]); x = sn[j + k], y = cn[i][k]; if(sc[j + k] == cc[i][k] && ( x == y || (x >= y && (k == 0 || cc[i][k + 1] == '\0')) ) ) { k++; } else { flag = false; break; } } if(flag) { if(k == 1) { ans += (x - y + 1); } else { ans++; } } j++; } } printf("%d\n", ans); return 0; }