加速C++;寻找最长的回文 我正在练习KoeEED加速C++,想验证我的答案。由于网络上没有可用的解决方案,我想把它贴在这里,并征求专家对我的解决方案的看法。我不确定人们是否会喜欢我把它贴在这里。如果没有,请让我知道,我不会在未来这样做。另外,它不是作业,它纯粹是我的愿望,把我的C++技能到下一级。
问题:编写一个程序来查找字典中的所有回文。接下来,找到最长的回文。 到目前为止我所做的->我定义了测试回文的函数,还将所有单词存储在一个列表中。我已经在下面发布了我的代码 我陷入困境的地方:我需要建议,我选择使用列表数据结构而不是向量是否正确?第二,我被困在如何显示最长的单词。我可以显示最长的长度,但不能显示最长的单词 下面是我的尝试加速C++;寻找最长的回文 我正在练习KoeEED加速C++,想验证我的答案。由于网络上没有可用的解决方案,我想把它贴在这里,并征求专家对我的解决方案的看法。我不确定人们是否会喜欢我把它贴在这里。如果没有,请让我知道,我不会在未来这样做。另外,它不是作业,它纯粹是我的愿望,把我的C++技能到下一级。,c++,stdvector,stdlist,C++,Stdvector,Stdlist,问题:编写一个程序来查找字典中的所有回文。接下来,找到最长的回文。 到目前为止我所做的->我定义了测试回文的函数,还将所有单词存储在一个列表中。我已经在下面发布了我的代码 我陷入困境的地方:我需要建议,我选择使用列表数据结构而不是向量是否正确?第二,我被困在如何显示最长的单词。我可以显示最长的长度,但不能显示最长的单词 下面是我的尝试 bool palindromeTest( const std::string& input ) { typedef std::string:
bool palindromeTest( const std::string& input )
{
typedef std::string::size_type strSize;
strSize i = 0;
strSize j = input.size() - 1 ;
while( i < input.size() )
{
if ( input[i] != input[j])
{
return false;
}
i++;
j--;
}
return true;
}
int main()
{
// stores all words in a list or vector
std::list< string> listDict;
std::string readWord;
std::ifstream readFile( "/Users/apple/palidndrome-ch5-10/dict.txt" );
if( ! readFile )
{
std::cout <<" failed to open file" << std::endl;
return 0;
}
while( readFile >> readWord )
{
listDict.push_back( readWord );
}
std::string::size_type maxLen = 0 ;
std::string longestWord = " "; // to store longest palindrome
// print all the palindrome words and also which is longest palindrome.
for( std::list<std::string>::const_iterator it = listDict.begin(); it != listDict.end(); ++it )
{
if( palindromeTest( *it ) )
{
std::cout <<" the word -> " << *it << " is palindrome" << std::endl;
// find max len of palindrome;
maxLen = max( maxLen, it->size() );
longestWord = *it ;// need to change code here ?? no idea how
}
}
std::cout <<" the maximum len is = " << maxLen << std::endl;
std::cout << " the word with maximum length is " << longestWord ; // something is wrong here
return 0;
}
bool回文测试(const std::string&input)
{
typedef std::string::size_type strSize;
标准化i=0;
strSize j=input.size()-1;
而(ilistDict;
std::字符串读取字;
std::ifstream readFile(“/Users/apple/paidndrome-ch5-10/dict.txt”);
如果(!readFile)
{
std::cout向量和列表在这里同样有效,尽管向量更有效
你可以通过改变单词来找到最长的单词
maxLen = max( maxLen, it->size() );
longestWord = *it ;// need to change code here ?? no idea how
到
(实际上,您不需要跟踪maxlen,因为您只需调用longestWord.size()首先是回文测试。您要检查字符串中的每个字符两次,这是不必要的。在这种特殊情况下,这无关紧要,因为它只是将特定操作的时间增加了一倍,对于一些类似的操作,它将改变语义(请考虑<代码>反向<代码> -在概念上非常类似于回文测试。为了改善检查,您可以使用一个循环计数器从<代码> 0代码/代码> <代码>输入。siz()/ 2 < /代码>或使用两个指针/迭代器,直到它们在中间相遇为止。还要注意,重复到<代码>输入。siz()/2
将留下一个从未对奇数大小的字符串进行过测试的字符,但这没关系
需要顺序容器时,应始终默认为std::vector
,而不是std::list
。要手动查找最大值,应将调用更改为max
,以进行测试,检查此元素是否大于先前的最大值,然后更新最大值和最大值的位置E字符串(或字符串本身)。在这种特殊情况下,如果重复次数高,也可以考虑不使用顺序容器,而是一个关联的容器(<代码> STD::SET),以避免重复。但是最好不要将单词存储在内存中。
您应该学习使用迭代器和标准库中的迭代器标头。例如,读取循环可以转换为:
std::vector<std::string> words;
std::copy( std::istream_iterator<std::string>( readFile ),
std::istream_iterator<std::string>(),
std::back_inserter( words );
通过提供适当的函子,还可以使用算法查找最长回文:
std::vector::const_迭代器max=
std::max_元素(words.begin(),words.end(),
[](标准::字符串常量和lhs,标准::字符串常量和rhs){
返回lhs.size()std::string longestPalindrome( std::istream& stream ) {
std::string longest;
std::for_each( std::istream_iterator<std::string>(stream),
std::istream_iterator<std::string>(),
[&longest]( std::string const & word ) {
// if is longest palindrome so far
if (std::equal( word.begin(),
word.begin()+word.size()/2,
word.rbegin()
&& word.size() > longest.size()))
{
longest = word;
}
});
return longest;
}
int main() {
std::ifstream readFile( "/Users/apple/palidndrome-ch5-10/dict.txt" );
if ( !readFile ) {
std::cerr << "failed to open file\n"; // [*]
exit(1);
}
std::string longest = longestPalindrome( readFile );
std::cout << "The longest palindrome is: '" << longest << "'\n";
}
即使在这种情况下,转储“\n”和
std::flush
也比std::endl
(不太清楚刷新是否是故意的,因为有太多的代码不停地使用std::endl
。我想指出,如果在阅读时检查每个单词,而不是将它们全部存储在一个巨大的容器中,你会为更长的词典节省多少内存。@chris,你能详细说明一下你所说的单词是什么意思吗在我看来,C++是一种新的东西。我喜欢分配任何内存。我的意思是:假设你的字典是100K字。现在你的方法是把它们全部存储到一个列表中。如果每个元素的指针指向字符的4字节,那就是列表中的400 K内存(没有实际的单词)。.现在,一次检查一个字符串并使用一个字符串保存当前字符串需要4个字节,但您需要做的只是检查它是否是回文,检查它是否是迄今为止最长的,然后继续。现在400K可能不重要,但最终您可能会遇到这样的情况,即您可以节省大量的钱。@chris,我知道了谢谢你的解释。我是在加速C++的第5章,我认为使用容器来存储理论上的值,并且习惯于使用容器函数,它们确实能很好地保存事物。这是一件值得思考的事情。这是思考事物的主要差异之一。您可以对所有内容执行步骤1),然后继续执行步骤2)等,也可以按顺序对每个内容执行所有步骤。根据具体情况,一个mig
bool isPalindrome( std::string const & str ) {
return std::equal( str.begin(), str.begin()+str.size()/2,
str.rbegin() );
}
std::string longestPalindrome( std::istream& stream ) {
std::string longest;
std::for_each( std::istream_iterator<std::string>(stream),
std::istream_iterator<std::string>(),
[&longest]( std::string const & word ) {
// if is longest palindrome so far
if (std::equal( word.begin(),
word.begin()+word.size()/2,
word.rbegin()
&& word.size() > longest.size()))
{
longest = word;
}
});
return longest;
}
int main() {
std::ifstream readFile( "/Users/apple/palidndrome-ch5-10/dict.txt" );
if ( !readFile ) {
std::cerr << "failed to open file\n"; // [*]
exit(1);
}
std::string longest = longestPalindrome( readFile );
std::cout << "The longest palindrome is: '" << longest << "'\n";
}
std::cout << "Enter a number: " << std::flush; // Ensure that the user gets the message
// before we lock waiting for an answer:
std::cin >> number;