-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDP_Abbreviation.cpp
108 lines (90 loc) · 2.33 KB
/
DP_Abbreviation.cpp
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
104
105
106
107
108
#include <bits/stdc++.h>
using namespace std;
bool isLower(char c) {
if (c >= 'a' && c <= 'z')
return true;
return false;
}
char toLower(char c){
return (char)(c-32);
}
// Complete the abbreviation function below.
string abbreviation(string a, string b) {
int m = a.size();
int n = b.size();
bool lcs[m+1][n+1];
for(int i=0;i<=m;i++)
for(int j=0;j<=n;j++)
lcs[i][j]=false;
lcs[0][0]=true;
for(int i = 1; i <= m; i++){
if(isLower(a[i-1])) lcs[i][0] = true;
else break;
}
for(int i = 1; i <=m; i++)
for(int j = 1; j <=n; j++){
if(isupper(a[i-1])){
if(a[i-1] == b[j-1] && lcs[i-1][j-1] )
lcs[i][j] = true;
else lcs[i][j] = false;
}
else{
if(toLower(a[i-1]) == b[j-1] && lcs[i-1][j-1]) lcs[i][j] = 1;
else lcs[i][j] = lcs[i-1][j];
}
}
/*
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++){
if(a[i-1] == b[j-1]){ //upper
// lcs[i][j] = true;
if(lcs[i-1][j-1])
lcs[i][j] = true;
else lcs[i][j] =false;
// lcs[i][j] = lcs[i-1][j-1];
}
else if ((isLower(a[i-1])) && (toLower(a[i-1])==b[j-1])) {
// lcs[i][j] = true;
if (lcs[i - 1][j - 1])
lcs[i][j] = true;
else
lcs[i][j] = lcs[i - 1][j];
//lcs[i][j] = lcs[i - 1][j - 1];
}
else if((!isLower(a[i-1])) && (a[i-1]!=b[i-1])){
lcs[i][j] = false;
//if (lcs[i - 1][j - 1])
// lcs[i][j] = true;
// else
// lcs[i][j] = false;
}
else{
lcs[i][j] = lcs[i - 1][j];
}
}*/
/*
for (int i = 0; i < m+1; i++) {
for (int j = 0; j < n+1; j++)
cout << lcs[i][j] << endl;
}*/
if (lcs[m][n])
return "YES";
return "NO";
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int q;
cin >> q;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int q_itr = 0; q_itr < q; q_itr++) {
string a;
getline(cin, a);
string b;
getline(cin, b);
string result = abbreviation(a, b);
fout << result << "\n";
}
fout.close();
return 0;
}