Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int n;
bool vis[101];
string s[101];
vector<string> sol;
int main() {
cin >> n;
for(int i = 0; i < n; ++i)
cin >> s[i];
for(int k = 0; k < 100; ++k) {
for(int i = 0; i < n; ++i) {
if(vis[i])
continue;
bool ok = true;
for(int j = 0; j < n; ++j) {
if(i == j || vis[j])
continue;
if(s[i].find(s[j]) == string::npos) {
ok = false;
break;
}
}
if(ok) {
sol.push_back(s[i]);
vis[i] = true;
}
}
}
if(sol.size() != n)
cout << "NO" << endl;
else {
cout << "YES" << endl;
for(int i = sol.size() - 1; i >= 0; --i)
cout << sol[i] << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
5
a
aba
abacaba
ba
aba
a
aba
abacaba
ba
aba
Output
YES
a
ba
aba
aba
abacaba
a
ba
aba
aba
abacaba
Demonstration
Codeforces Solution-B. Substrings Sort-Solution in C, C++, Java, Python,Substrings Sort,Codeforces Solution