-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcustomised string sort.cpp
71 lines (63 loc) · 1.87 KB
/
customised string sort.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
/*Nishant is a very naughty boy in Launchpad Batch. One day he was playing with strings,
and randomly shuffled them all. Your task is to help Nishant Sort all the strings ( lexicographically )
but if a string is present completely as a prefix in another string, then string with longer length should come first.
Eg bat, batman are 2 strings and the string bat is present as a prefix in Batman - then sorted order should have - Batman, bat.
Input Format
N(integer) followed by N strings.
Constraints
N<=1000
Output Format
N lines each containing one string.
Sample Input
3
bat
apple
batman
Sample Output
apple
batman
bat
Explanation
In mathematics, the lexicographic or lexicographical order (also known as lexical order, dictionary order, alphabetical order or lexicographic(al) product)
is a generalization of the way words are alphabetically ordered based on the alphabetical order of their component letters.
*/
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<bits/stdc++.h>
using namespace std;
/*
let say we have given 2 strings a,b;
we have to sheck for 3 conditions
1.if a is prefix string of b then b becomes first.
2.if b is prefix string of a then a becomes first.
3.if above 2 conditions dont satisfy then sort them in lexographical order.
*/
//comperator function for sorting
bool comp(string a, string b)
{
//a will only be the substring of b when a is smaller or equal to b and a is the prefix substring of b .
if(a.size()<=b.size()&&b.substr(0,a.size())==a)return false;
else if(b.size()<=a.size()&&a.substr(0,b.size())==b)return true;
else return a<b;
}
int main()
{
//vector of string
vector<string>v;
string s;
int n,i;
cin>>n;
for(i=0;i<n;i++)
{
cin>>s;
v.push_back(s);
}
//sorting using comparator function.
sort(v.begin(),v.end(),comp);
//printing the output
for(auto x:v)
cout<<x<<endl;
return 0;
}