-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongest_String _Chain.cpp
93 lines (77 loc) · 2 KB
/
Longest_String _Chain.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
// Mr Saurabh is given a list of words.
// Your task is to help Mr Saurabh to find the size of largest group
// A group is formed using the following rules:
// - Pick one smallest word (in length) and form a group with this word
// (if it is not part of any other group yet)
// - Add a letter at any place in the word without changing the relative order
// of the letters in it, and if it forms a word which is existing in the list[],
// then add the word to the group.
// - Repeat the above two steps, till all the words in the list are part of groups.
// NOTE:You move form more than one group.
// Input Format:
// -------------
// Space separated words, wordsList[].
// Output Format:
// --------------
// Print an integer result
// Sample Input-1:
// ---------------
// many money n an mony any one mone on
// Sample Output-1:
// ----------------
// 5
// Explanation:
// ------------
// the words group is : [n, on, one, mone, money]
// Sample Input-2:
// ---------------
// a ab abb babb abab baba bab abbaa
// Sample Output-2:
// ----------------
// 4
#include<bits/stdc++.h>
using namespace std;
bool isSubSequence(string a,string b){
int i=0,j=0;
while(i<a.size() && j<b.size()){
if(a[i]==b[j]){
i++;
j++;
}
else{
i++;
}
}
return j==b.size();
}
int main(){
string s;
getline(cin,s);
vector<string> v;
stringstream x1(s);
string t1="";
while(getline(x1,t1,' ')){
v.push_back(t1);
}
sort(v.begin(),v.end(),[&](string a,string b){
if(a.size()<b.size()){
return true;
}
else{
return false;
}
});
int maxi=1;
vector<int> dp(v.size(),1);
for(int i=1;i<v.size();i++){
for(int j=0;j<i;j++){
if(v[i].size()-v[j].size()==1){
if(isSubSequence(v[i],v[j])){
dp[i]=max(dp[i],dp[j]+1);
}
}
}
maxi=max(maxi,dp[i]);
}
cout<<maxi;
}