Given a string S and a set of words D, find the longest word in D that is a subsequence of S
$begingroup$
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 <iostream>
#include <string>
#include <tuple>
#include <vector>
class StringManipulater {
public:
StringManipulater(const std::string& p) : given_string_{p} {}
std::string given_string_;
std::tuple<std::string, typename std::string::size_type> current_longest_ = {
"", 0};
const std::string&& FindLongestSubSeq(const std::vector<std::string>& 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& 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 > std::get<1>(this->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 < word_len; ++i) {
// no need to go further checking if we have iterated over given
// string
if (i == this->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<0>(this->current_longest_) = word;
std::get<1>(this->current_longest_) = word_len;
}
} // top if
} // end of iteration of dictionary words
return std::move(std::get<0>(this->current_longest_));
}
};
int main()
{
StringManipulater s_manip{"abppplee"};
std::cout << "Longest subsequence = "
<< s_manip.FindLongestSubSeq(
{"able", "ale", "apple", "bale", "kangaroo"})
<< "n";
return 0;
}
c++ performance algorithm c++11
New contributor
$endgroup$
add a comment |
$begingroup$
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 <iostream>
#include <string>
#include <tuple>
#include <vector>
class StringManipulater {
public:
StringManipulater(const std::string& p) : given_string_{p} {}
std::string given_string_;
std::tuple<std::string, typename std::string::size_type> current_longest_ = {
"", 0};
const std::string&& FindLongestSubSeq(const std::vector<std::string>& 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& 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 > std::get<1>(this->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 < word_len; ++i) {
// no need to go further checking if we have iterated over given
// string
if (i == this->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<0>(this->current_longest_) = word;
std::get<1>(this->current_longest_) = word_len;
}
} // top if
} // end of iteration of dictionary words
return std::move(std::get<0>(this->current_longest_));
}
};
int main()
{
StringManipulater s_manip{"abppplee"};
std::cout << "Longest subsequence = "
<< s_manip.FindLongestSubSeq(
{"able", "ale", "apple", "bale", "kangaroo"})
<< "n";
return 0;
}
c++ performance algorithm c++11
New contributor
$endgroup$
add a comment |
$begingroup$
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 <iostream>
#include <string>
#include <tuple>
#include <vector>
class StringManipulater {
public:
StringManipulater(const std::string& p) : given_string_{p} {}
std::string given_string_;
std::tuple<std::string, typename std::string::size_type> current_longest_ = {
"", 0};
const std::string&& FindLongestSubSeq(const std::vector<std::string>& 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& 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 > std::get<1>(this->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 < word_len; ++i) {
// no need to go further checking if we have iterated over given
// string
if (i == this->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<0>(this->current_longest_) = word;
std::get<1>(this->current_longest_) = word_len;
}
} // top if
} // end of iteration of dictionary words
return std::move(std::get<0>(this->current_longest_));
}
};
int main()
{
StringManipulater s_manip{"abppplee"};
std::cout << "Longest subsequence = "
<< s_manip.FindLongestSubSeq(
{"able", "ale", "apple", "bale", "kangaroo"})
<< "n";
return 0;
}
c++ performance algorithm c++11
New contributor
$endgroup$
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 <iostream>
#include <string>
#include <tuple>
#include <vector>
class StringManipulater {
public:
StringManipulater(const std::string& p) : given_string_{p} {}
std::string given_string_;
std::tuple<std::string, typename std::string::size_type> current_longest_ = {
"", 0};
const std::string&& FindLongestSubSeq(const std::vector<std::string>& 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& 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 > std::get<1>(this->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 < word_len; ++i) {
// no need to go further checking if we have iterated over given
// string
if (i == this->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<0>(this->current_longest_) = word;
std::get<1>(this->current_longest_) = word_len;
}
} // top if
} // end of iteration of dictionary words
return std::move(std::get<0>(this->current_longest_));
}
};
int main()
{
StringManipulater s_manip{"abppplee"};
std::cout << "Longest subsequence = "
<< s_manip.FindLongestSubSeq(
{"able", "ale", "apple", "bale", "kangaroo"})
<< "n";
return 0;
}
c++ performance algorithm c++11
c++ performance algorithm c++11
New contributor
New contributor
New contributor
asked 1 min ago
YedhinYedhin
1011
1011
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Yedhin is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211801%2fgiven-a-string-s-and-a-set-of-words-d-find-the-longest-word-in-d-that-is-a-subs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yedhin is a new contributor. Be nice, and check out our Code of Conduct.
Yedhin is a new contributor. Be nice, and check out our Code of Conduct.
Yedhin is a new contributor. Be nice, and check out our Code of Conduct.
Yedhin is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211801%2fgiven-a-string-s-and-a-set-of-words-d-find-the-longest-word-in-d-that-is-a-subs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown