-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacmaker.cpp
More file actions
71 lines (68 loc) · 1.63 KB
/
acmaker.cpp
File metadata and controls
71 lines (68 loc) · 1.63 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
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define pb push_back
#define mp make_pair
typedef long long ll;
int dp[50][50][50];
string toLowerC(string s) {
for (int i=0;i<s.length();i++) s[i]^=32;
return s;
}
int findWays(string &ab,vector<string> &words,int i,int j,int l) {
if(i==ab.length()&&j==words.size()-1)
return 1;
if(i==ab.length()||j==words.size())
return 0;
if(dp[i][j][l]!=-1)
return dp[i][j][l];
string s=words[j];
int ans=0;
for(int k=l;k<s.size();k++) {
if(ab[i]==s[k]) {
ans+=findWays(ab,words,i+1,j,k+1);
}
}
if(l)
ans+=findWays(ab,words,i,j+1,0);
return dp[i][j][l]=ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
while(1) {
int n;
cin>>n;
if(n==0)
break;
set<string> insignificant;
for(int i=0;i<n;i++) {
string s1;
cin>>s1;
insignificant.insert(s1);
}
int f=0;
while(1) {
string acro; cin >> acro; cin.ignore();
string phrase; getline(cin, phrase);
if (phrase == "CASE") break;
istringstream iss(phrase);
vector<string> words;
for (;;) {
string word; iss >> word;
if (word == "") break;
if (insignificant.find(word) == insignificant.end()) {
words.push_back(word);
}
}
string ab=toLowerC(acro);
memset(dp,-1,sizeof(dp));
int ans=findWays(ab,words,0,0,0);
if(ans)
cout<<acro<<" can be formed in "<<ans<<" ways\n";
else
cout<<acro<<" is not a valid abbreviation\n";
}
}
return 0;
}