Given a string S and a set of words D, find the longest word in D that is a subsequence of S I'm trying to improve my code and algorithm to find out the longest word from a dictionary occurring as the sub-sequence in the given string.  

Example : For example, given the input of S = "abppplee" and D = {"able", "ale", "apple", "bale", "kangaroo"} the correct output would be "apple".  

Currently my algorithm compares each character of the dictionary-word with the given-word and advances to next char in dictionary-word on finding a match and so on till either the dictionary-word is iterated over completely(which means the word is a valid sub-sequence) or the given word is iterated over completely(which means not a valid string). 

Being a beginner to c++ and algorithms in general, my methods and logic could be further improved. So kindly suggest if any improvements in the algorithm or code which can improve the performance. Thanks.  

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;tuple&gt;
#include &lt;vector&gt;

class StringManipulater {
  public:
  StringManipulater(const std::string&amp; p) : given_string_{p} {}
  std::string given_string_;
  std::tuple&lt;std::string, typename std::string::size_type&gt; current_longest_ = {
      "", 0};

  const std::string&amp;&amp; FindLongestSubSeq(const std::vector&lt;std::string&gt;&amp; dict)
  {
    // flag determines whether current word is present in given_string input
    bool flag;

    // iterate through each of the dict-words
    for (const std::string&amp; word : dict) {
      flag = false;
      auto word_len = word.size();

      // fair optimization : only consider current word if its
      // length is greater than previous one
      if (word_len &gt; std::get&lt;1&gt;(this-&gt;current_longest_)) {
        // iterate through the letters in both given string and current word
        // to be compared
        for (typename std::string::size_type i = 0, j = 0; j &lt; word_len; ++i) {
          // no need to go further checking if we have iterated over given
          // string
          if (i == this-&gt;given_string_.size()) break;

          // compare each letter of both words
          if (word[j] == given_string_[i]) {
            flag = true;
            // advance to next charcter in the dictionary word
            ++j;
          }
          else {
            // if the char couldn't be found
            flag = false;
          }
        }  // end of comparison loop
        if (flag) {
          std::get&lt;0&gt;(this-&gt;current_longest_) = word;
          std::get&lt;1&gt;(this-&gt;current_longest_) = word_len;
        }
      }  // top if
    }    // end of iteration of dictionary words
    return std::move(std::get&lt;0&gt;(this-&gt;current_longest_));
  }
};

int main()
{
  StringManipulater s_manip{"abppplee"};
  std::cout &lt;&lt; "Longest subsequence = "
            &lt;&lt; s_manip.FindLongestSubSeq(
                   {"able", "ale", "apple", "bale", "kangaroo"})
            &lt;&lt; "\n";
  return 0;
}
You know, if you want to improve performance, the first thing to do is get a high-level overview of what your code does to achieve its aim. Currently, you do this:


Iterate over all words in D.


Iterate over the whole string S trying to prove it a sub-sequence.



That's trivially proven to be an \$O(\#D * \#S)\$ algorithm.

You can do better, by using the proper data structure:


Build a prefix-tree from the words, noting the longest word which can be found from there.
Iterate over the string.


Continue if the current character doesn't fit.
Update the best found if it is a terminal node.
recurse with the sub-tree.



Regarding your code:


Your use of a class is completely unmotivated. There are no invariants to protect, as evidenced by you marking it all public, and you only create it to call one single function anyway. Just make it a free function. Ok, if you change the algorithm, you might want to break it up it into building the trie, using a pre-computed trie, and putting it all together.
auto would allow you to always have the proper type, without cluttering your code with long and obscure names. Read "Almost Always Auto".
I'm not sure why you use a std::tuple. Maybe for obfuscation? If so, I confirm you are on the right way.
Always define variables in the smallest scope possible, unless there are significant advantages (most likely in performance) for a slightly longer lifetime.
return 0; is implicit for main().