延伸至Palindrome

时间:2017-10-15 11:10:25

标签: c++ string c++11

为了解决UVa 11475 - Extend to Palindrome,我提出了以下算法:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
    string word;
    while (cin >> word) {
        unsigned long i(0), j(word.size() - 1);
        while (i < j) {
            if (word[i] != word[j])
                word.insert(j + 1, 1, word[i]);
            else
                j--;
            i++;
        }
        cout << word << endl;
    }
    return 0;
}

但是结果是“错误答案”,尽管代码为样本输入提供了正确的输出。那我在这里错过了什么?什么样的测试案例可能导致这种情况?

我的教授告诉我们检查KMP算法,作为提示,但我不明白字符串搜索算法与检查字符串是否为回文有什么关系。

1 个答案:

答案 0 :(得分:-1)

例如,在测试中检查您的程序

  

adbad

它给出了错误的答案