-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_break.cpp
49 lines (41 loc) · 925 Bytes
/
word_break.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
/*
Given a string A and a dictionary of n words B, find out if A can be segmented into a space-separated sequence
of dictionary words.
*/
#include <bits/stdc++.h>
using namespace std;
bool WordBreak(string& a, vector<string> &words){
//first we put all words in a unordered-set
unordered_set<string> mp;
for(auto &x: words){
mp.insert(x);
}
int n = a.length();
//dp[i] stores true if it is possible to segment string s[0...(i-1)]
//into sentence of words present in the dictionary
vector<bool> dp(n+1, false);
dp[0] = true;
for(int i = 1; i <= n; i++){
for(int j = i-1; j >= 0; j--){
if(dp[j] && mp.find(a.substr(j, i-j)) != mp.end()){
dp[i] = true;
break;
}
}
}
return dp[n];
}
int main(){
int n;
cin >> n;
vector<string> words;
for(int i = 0; i < n; i++){
string w;
cin >> w;
words.push_back(w);
}
string a;
cin >> a;
cout << WordBreak(a, words) << "\n";
return 0;
}