forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_sentence.py
45 lines (37 loc) · 975 Bytes
/
make_sentence.py
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
"""
For a given string and dictionary, how many sentences can you make from the
string, such that all the words are contained in the dictionary.
// eg: for given string -> "appletablet"
// "apple", "tablet"
// "applet", "able", "t"
// "apple", "table", "t"
// "app", "let", "able", "t"
// "applet", {app, let, apple, t, applet} => 3
// "thing", {"thing"} -> 1
"""
// Example program
#include <iostream>
#include <string>
#include <set>
#include <vector>
using namespace std;
bool foo(string s, set<string> dic, int &cnt){
if(s.empty())
return true;
for(int i=0;i<s.length();++i){
string prefix = s.substr(0,i), suffix = s.substr(i);
if((dic.count(prefix)&&dic.count(suffix))
||(dic.count(prefix)&&foo(suffix,dic,cnt))){
cnt++;
}
}
return true;
}
int main()
{
string s = "applet";
set<string> dic{"","app","let","t","apple","applet"};
int cnt = 0;
foo(s,dic,cnt);
cout<<cnt;
}