forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA.cpp
More file actions
103 lines (84 loc) · 2.66 KB
/
A.cpp
File metadata and controls
103 lines (84 loc) · 2.66 KB
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
95
96
97
98
99
100
101
102
103
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
using namespace std;
int nBad;
char tmp[100111];
vector<string> words;
string need;
int f[211][211];
int g[211][211];
void solve() {
// DEBUG(need);
// for(auto s : words) cout << s << ' ';
// cout << endl;
memset(f, 0, sizeof f);
f[0][0] = 1;
FOR(i,0,words.size()-1) FOR(j,0,need.size()-1) if (f[i][j]) {
// now we use the (i+1)th word
int l = words[i].length();
int maxk = min((int) need.size()-j, l);
FOR(x,0,l) FOR(y,0,l) g[x][y] = 0;
FOR(x,0,l) FOR(y,0,maxk) {
if (x == 0 && y == 0) {
g[x][y] = 1;
}
else if (x == 0) {
g[x][y] = 0;
}
else {
g[x][y] = g[x-1][y];
if (y > 0 && words[i][x-1] == need[j+y-1])
g[x][y] += g[x-1][y-1];
}
}
// if we use k character --> g[l][k]
FOR(k,1,l)
if (g[l][k])
f[i+1][j+k] += f[i][j] * g[l][k];
}
int res = f[words.size()][need.size()];
if (!res) {
printf("%s is not a valid abbreviation\n", need.c_str());
}
else {
printf("%s can be formed in %d ways\n", need.c_str(), res);
}
}
int main() {
ios :: sync_with_stdio(false);
while (scanf("%d\n", &nBad) == 1 && nBad) {
set<string> bad;
FOR(i,1,nBad) {
gets(tmp);
REP(i,strlen(tmp))
if (tmp[i] >= 'a' && tmp[i] <= 'z')
tmp[i] ^= ' ';
bad.insert(string(tmp));
}
while (true) {
gets(tmp);
REP(i,strlen(tmp))
if (tmp[i] >= 'a' && tmp[i] <= 'z')
tmp[i] ^= ' ';
string s (tmp);
if (s == "LAST CASE") break;
stringstream ss(s);
string t;
words.clear();
ss >> need;
while (ss >> t) {
if (bad.count(t)) continue;
words.push_back(t);
}
solve();
}
}
return 0;
}