forked from rishabhgarg25699/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword_break.cpp
More file actions
43 lines (38 loc) · 673 Bytes
/
word_break.cpp
File metadata and controls
43 lines (38 loc) · 673 Bytes
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
#include <iostream>
#include<vector>
#include<unordered_set>
#include<string>
using namespace std;
unordered_set<string>dict;
bool func(string &s, int i, vector<int>&dp) {
if (i == s.length())
return 1;
if (dp[i] != -1)
return dp[i];
for (int j = i + 1; j <= s.length(); ++j) {
if (dict.find(s.substr(i, j - i)) != dict.end()) {
if (func(s, j, dp)) {
return dp[i] = 1;
}
}
}
return dp[i] = 0;
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
string a, s;
cin >> n;
dict.clear();
while (n--) {
cin >> a;
dict.insert(a);
}
cin >> s;
vector<int>dp(s.length(), -1);
cout << func(s, 0, dp) << endl; <br>
}
return 0;
}