#include #include #include #include using namespace std; int n; string word; string dict[60]; int memo2[51][51]; int memo[51][51][51][51]; int Vanishes(int i1, int i2); int Vanishes(int i1, int i2, int w, int wp) { if (memo[i1][i2][w][wp] != -1) return (memo[i1][i2][w][wp] == 1); if (wp == dict[w].size()) return Vanishes(i1, i2); int leeway = (i2 - i1 + 1) - (int(dict[w].size()) - wp); for (int j = i1; j <= i1 + leeway; j++) if (word[j] == dict[w][wp]) if (Vanishes(i1, j-1) && Vanishes(j+1, i2, w, wp+1)) return (memo[i1][i2][w][wp] = 1); return (memo[i1][i2][w][wp] = 0); } int Vanishes(int i1, int i2) { if (i1 > i2) return true; if (memo2[i1][i2] != -1) return (memo2[i1][i2] == 1); for (int w = 0; w < n; w++) if (i2 - i1 + 1 >= int(dict[w].size()) && Vanishes(i1, i2, w, 0)) return (memo2[i1][i2] = 1); return (memo2[i1][i2] = 0); } int Solve() { vector best(51); best[0] = 0; for (int i = 1; i <= int(word.size()); i++) { best[i] = best[i-1] + 1; for (int j = 0; j < i; j++) if (Vanishes(j, i-1)) best[i] = min(best[i], best[j]); } return best[word.size()]; } int main() { while ((cin >> n) && (n != 0)) { cin >> word; for (int i = 0; i < n; i++) cin >> dict[i]; memset(memo, -1, 51*51*51*51*sizeof(int)); memset(memo2, -1, 51*51*sizeof(int)); cout << Solve() << endl; } return 0; }