-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPassword Cracker
More file actions
64 lines (54 loc) · 1.36 KB
/
Copy pathPassword Cracker
File metadata and controls
64 lines (54 loc) · 1.36 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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int N;
int dictionaryContains(string word,string dictionary[])
{
//string dictionary[] = {"because","can","do","must","we","what"};
//int size = sizeof(dictionary)/sizeof(dictionary[0]);
for (int i = 0; i < N; i++)
if (dictionary[i].compare(word) == 0)
return true;
return false;
}
bool wordBreak(string str,string result,string dic[])
{
int size = str.size();
// Base case
if (size == 0) return true;
// Try all prefixes of lengths from 1 to size
for (int i=1; i<=size; i++)
{string prefix = str.substr(0, i);
if (dictionaryContains( str.substr(0, i),dic ) &&
wordBreak( str.substr(i, size-i),result + prefix + " ",dic )){
if (i == size)
{
// add this element to previous prefix
result += prefix;
cout << result << endl; //print result
}
return true;
}
}
// If we have tried all prefixes and none of them worked
return false;
}
int main() {
int T,i;
cin>>T;
string str;
while(T--) {
cin>>N;
string dic[N];
for(i=0;i<N;i++)
cin>>dic[i];
cin>>str;
wordBreak(str,"",dic)?:cout<<"WRONG PASSWORD\n";
//cout<<endl;
}
return 0;
}