988B - Substrings Sort - CodeForces Solution


sortings strings *1100

Please click on ads to support us..

Python Code:

qtd = int(input())
listat = list()
listap = list()
listar = list()
x = True

for i in range(0,qtd):
    p = input()
    listap.append(p)

listap = sorted(listap)


for i in range(0, len(listap)):
    word = listap[i]
    tam = len(word)
    listat.append(tam)

listat = sorted(listat)

for i in range(0, len(listap)):
    word = listap[i]
    tam = len(word)
    for j in range(0, len(listap)):
        if tam == listat[j]:
            listat[j] = word
            break


listar.append('YES')       
for i in range (0, qtd-1):
    primeira = listat[i]
    outra = listat[i+1]
    
    if outra.find(primeira) != -1:
        listar.append(primeira)
    else:
        print("NO")
        x = False
        break


if x == True:
    listar.append(listat[len(listat)-1])
    for i in range(0,len(listar)):
        print(listar[i])

		 		    	    		  	    		  	 		

C++ Code:

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> P;
#define all(x) (x).begin(), (x).end()
#define loop(a, n) for (ll a = 0; a < (n); ++a)
#define vloop(a, v) for (auto a = v.begin(); a != v.end(); ++a)
#define INF (ll)INFINITY
#define F first
#define S second
#define pb emplace_back
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;

bool check(string s, string t){
	if(s.size() == t.size() && s.compare(t) == 0) return true;
	loop(i, t.size() - s.size() + 1){
		string r = t.substr(i, s.size());
		if(s.compare(r) == 0) return true;
	}
	return false;
}
void solve(){
	int n;
	cin >> n;
	vector<string> st(n);
	loop(i, n) cin >> st[i];
	sort(all(st), [=](string s, string t){
	      if(s.size() == t.size()){
		      int k = 0;
		      while(k < s.size() && s[k] == t[k]) k++;
		      return s[k] < t[k];
	      }
	      return s.size() < t.size();
	});
	/* loop(i, n) cout << st[i] << " "; */
	loop(i, n-1){
		string s = st[i], t = st[i+1];
		/* cout << s << " " << t << " "; */
		if(!check(s, t)){
			cout << "NO";
			return;
		}
	}
	cout << "YES" << "\n";
	loop(i, n) cout << st[i] << "\n";
}

int main(){
	#ifndef ONLINE_JUDGE
	freopen("inp","r",stdin);
	#endif
	/* int t; */
	/* scanf("%d", &t); */
	/* while(t--) */
		solve();
}


Comments

Submit
0 Comments
More Questions

AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible