fork(2) download
  1.  
  2. //KUNAL FARMAH
  3. //@kunalfarmh98@gmail.com
  4.  
  5. #include<bits/stdc++.h>
  6. using namespace std;
  7.  
  8. bool compare(string a, string b){
  9. int n = a.length();
  10. int m = b.length();
  11.  
  12. int i=0;
  13. for(; i<n; i++){
  14. if(a[i]!='0')
  15. break;
  16. }
  17.  
  18. string temp="";
  19. if(i!=0){
  20. for(;i<n;i++)
  21. temp+=a[i];
  22.  
  23. a=temp;
  24. n = a.length();
  25. }
  26.  
  27.  
  28.  
  29. i=0;
  30. for(; i<m; i++){
  31. if(b[i]!='0')
  32. break;
  33. }
  34.  
  35. temp="";
  36. if(i!=0){
  37. for(;i<m;i++)
  38. temp+=b[i];
  39.  
  40. b=temp;
  41. m = b.length();
  42. }
  43.  
  44.  
  45.  
  46. if(n<m || a==b)return true;
  47. else if(n>m) return false;
  48. else{
  49. for(int i=0; i<n; i++){
  50. if(a[i]-'0'<b[i]-'0')return true;
  51. else if(a[i]-'0'>b[i]-'0')return false;
  52. }
  53. }
  54. }
  55.  
  56.  
  57. int search(string a[], int n, string val){
  58. string temp="";
  59. //int ind = 0;
  60. for(int i=0; i<n; i++){
  61. for(int j=0; j<a[i].length(); j++){
  62. if(a[i][j]==' ' && temp==val)
  63. return i;
  64. else if(a[i][j]==' '&&temp!=val) {
  65. temp = "";
  66. continue;
  67. }
  68.  
  69. temp+=a[i][j];
  70. }
  71. }
  72. }
  73. int main(){
  74. int n;
  75. cin>>n;
  76. cin.ignore();
  77. //cout<<n<<endl;
  78.  
  79. string a[n];
  80. for(int i=0; i<n; i++){
  81. string s;
  82. getline(cin,s);
  83. s+=' ';
  84. //cin.ignore();
  85. a[i]=s;
  86. }
  87.  
  88. int key;
  89. string rev;
  90. string type;
  91. cin>>key>>rev>>type;
  92.  
  93. // for(int i=0; i<n; i++){
  94. // cout<<a[i]<<endl;
  95. // }
  96.  
  97. int c=0;
  98.  
  99. int s= a[0].length();
  100. for(int i=0; i<s; i++){
  101. if(a[0][i]==' ')++c;
  102. }
  103.  
  104. //cout<<c<<endl;
  105. //separating columns
  106. vector<string> in[c+1];
  107.  
  108.  
  109. for(int i=0; i<n; i++){
  110. string temp="";
  111. int j=0;
  112. for(int k=0; k<a[i].length(); k++){
  113. if(a[i][k]==' '){
  114. in[j++].push_back(temp);
  115. temp="";
  116. continue;
  117. //++j;
  118. }
  119. temp+=a[i][k];
  120. }
  121. }
  122.  
  123.  
  124. // picking the correct column and sorting it
  125. vector<string> v = in[key-1];
  126. if(type=="lexicographical"){
  127. sort(v.begin(),v.end());
  128. }
  129. else{
  130. sort(v.begin(),v.end(),compare);
  131. }
  132.  
  133. if(rev=="true")
  134. reverse(v.begin(),v.end());
  135.  
  136. // printing the complete string according to the order given by the sorting
  137. for(string str:v){
  138. cout<<a[search(a,n,str)]<<endl;
  139. }
  140.  
  141.  
  142.  
  143. return 0;
  144. }
Success #stdin #stdout 0s 4384KB
stdin
3
92 022
82 12
77 13
2 false numeric
stdout
82 12 
77 13 
92 022