Tuesday, 1 November 2016

C++:Printing the kth smallest suffix for a string

Prequisistes

  • Stl vector
  • std::sort() for vector
  • Pointers

Code 

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

vector<string >v;
//s is the string for which the suffix is to be found out
string s;
int main() {

 int k;//kth smallest string
 string suffix;
 cin>>s>>k;
 int l=s.length();
 for(int i=1;i<=l;i++){
   suffix = string(&s[l-i]);
   v.push_back(suffix);
 }
 sort(v.begin(),v.end());
 cout<<v[k-1];
 return 0;
}

No comments:

Post a Comment