File size: 2,048 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<math.h>
#include<sstream>

#define sd(x) scanf("%d",&x);
#define sd2(x,y) scanf("%d %d",&x,&y);
#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z);
#define sull(x) scanf("%ull",&x);
#define print(x) printf("%d\n",x);
#define print2(x,y) printf("%d %d\n",x,y);
#define print3(x,y,z) printf("%d %d %d\n",x,y,z);
#define printull(x) printf("%ull\n",x);

using namespace std;

// Uglyyyyyyyyyyyy

int main(){
	int n, i, j, k, dp[151][151], count[151][151], w;
	count[0][0] = 1;
	for(i = 1; i <= 150; i++)
		count[0][i] = 0;
	for(i = 1; i <= 150; i++)
		count[i][0] = 1;
	string input, acronym, up_acronym, word;
	set<string> insig;
	vector<string> words;
	sd(n);
	while(n != 0){
		insig.clear();
		for(i = 0; i < n; i++){
			cin>>word;
			insig.insert(word);
		}
		getline(cin,input);
		while(getline(cin,input) and input != "LAST CASE"){
			words.clear();
			stringstream ss;
			
			ss<<input;
			ss>>up_acronym;
			acronym = "";
			for(i = 0; i < up_acronym.length(); i++) acronym += tolower(up_acronym[i]);
			
			
			while(ss>>word){
				if(insig.find(word) == insig.end()){
					words.push_back(word);
				}
			}
			
			int l1 = words.size(), l2 = acronym.length()	;
			
			for(i = 0; i <= l1; i++) 
				for(j = 0; j <= l2; j++) 
					dp[i][j] = 0;
			
			dp[0][0] = 1;
			
			for(i = 0; i < l1; i++){
				for(j = 1; j <= l2; j++){
					for(k = 1; k <= l2 - j + 1; k++){
					
						for(int p = 1; p <= words[i].length(); p++){
							for(int q = 1; q <= k; q++){
								count[p][q] = count[p-1][q];
								if(words[i][p-1] == acronym[j+q-2]) count[p][q] += count[p-1][q-1];
							}
						}
						w = count[words[i].length()][k];
						dp[i+1][j+k-1] += w * dp[i][j-1];
					}
				}
			}
			if(dp[l1][l2]){
				cout<<up_acronym<<" can be formed in "<<dp[l1][l2]<<" ways"<<endl;
			}
			else{
				cout<<up_acronym<<" is not a valid abbreviation"<<endl;
			}
		}
		sd(n);
	}
	return 0;
}