加速C++以找到最长的回文

accelerated C++ to find longest of palindrome

本文关键字:回文 C++ 加速      更新时间:2023-10-16

我正在练习Koeing加速C++,并想验证我的答案。由于网上没有可用的解决方案,我想把它贴在这里,并询问专家对我的解决方案的看法。我不确定人们是否希望我在这里发布它。如果没有,请告诉我,我以后不会这样做。另外值得一提的是,这不是家庭作业,纯粹是我希望将我的C++技能提升到一个新的水平。

问题:编写一个程序来查找字典中的所有回文。接下来,找到最长的回文。

到目前为止,我所做的 ->我已经定义了测试回文的函数,并将所有单词存储在列表中。我在下面发布了我的代码。

卡住的地方:我需要建议,我选择使用列表数据结构而不是向量是否好?其次,我被困在如何显示最长的单词。我可以显示最长的长度,但不能显示最长的单词。

我在下面的尝试

    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;
}

向量和列表在这里同样有效,尽管向量更有效。

您可以通过更改找到最长的单词

maxLen = max( maxLen, it->size() );
            longestWord = *it ;// need to change code here ?? no idea how

if (it->size() >= longestWord.size()) {longestWord = *it;}

(你实际上不需要跟踪maxlen,因为你只需要调用longestWord.size((。

首先是回文测试。您正在检查字符串中的每个字符两次,这是不需要的。虽然在这种特殊情况下这无关紧要,因为它只是使该特定操作的时间加倍,但对于一些类似的操作,它将改变语义(考虑reverse - 概念上与回文测试非常相似(。为了改进检查,您可以使用从0input.size()/2的循环计数器,或者使用两个指针/迭代器并运行直到它们在中间相遇。另请注意,向上迭代到 input.size()/2 将留下一个从未测试过奇数大小字符串的字符,但这很好。

当需要顺序容器时,应始终默认为 std::vector<> ,而不是 std::list<> 。若要手动查找最大值,应将测试的调用更改为 max,该测试检查此元素是否大于上一个最大值,然后更新 max 的值和字符串(或字符串本身(的位置。在这种特殊情况下,如果重复次数很高,你也可以考虑不使用顺序容器,而是使用关联容器(std::set<std::string>(以避免重复。但最好不要将这些单词完全存储在内存中。

您应该学习使用迭代器,以及标准库中迭代器的标头。例如,读取循环可以转换为:

std::vector<std::string> words;
std::copy( std::istream_iterator<std::string>( readFile ),
           std::istream_iterator<std::string>(),
           std::back_inserter( words );

回文的检查可以是对std::equal算法的调用:

bool isPalindrome( std::string const & str ) {
   return std::equal( str.begin(), str.begin()+str.size()/2,
                      str.rbegin() );
}

查找最长的回文也可以通过算法来完成,通过提供适当的函子:

std::vector::const_iterator max = std::max_element( words.begin((, words.end((, []( std::string const & lhs, std::string const & rhs ( { 返回 lhs.size((

(使用 C++11 中的 lambda,在 C++03 中,您需要创建一个执行相同比较的函子,这需要更多的样板代码,但不应该更复杂(

正如Antimony指出的那样,如果你只需要这个结果,你不需要将所有单词都保存在内存中,在这种情况下,你可以只测试每个单词的读数,并且只有在它是迄今为止最长的回文时才存储它。这在标准算法中并不容易实现,只需滚动自己的循环会更简单。

虽然出于学习目的,您可能希望遵循本书(针对C++03(,但C++11中的简单解决方案可能是:

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 filen";      // [*]
      exit(1);
   }
   std::string longest = longestPalindrome( readFile );
   std::cout << "The longest palindrome is: '" << longest << "'n";
}

[*]:注意,除非你真的需要它,否则最好只输出""而不是std::endl。两者之间的区别在于,std::endl 还会刷新流(强制写入(,这将无缘无故地影响性能。至于什么时候需要刷新,基本上是什么时候需要确保现在生成输出,比如查询用户时:

std::cout << "Enter a number: " << std::flush; // Ensure that the user gets the message
                                               // before we lock waiting for an answer:
std::cin >> number;

即使在这种情况下,转储""和std::flush也比std::endl更清楚(如果您需要换行符((其中不太清楚刷新是故意的,因为有太多的代码无休止地使用std::endl......