简体   繁体   English

找到两个字符串C ++之间的最长公共后缀

[英]Finding the longest common suffix between two strings C++

I'm fairly new to C++ and I can't seem to figure this out. 我对C ++很新,我似乎无法解决这个问题。

I get some weird output when running it. 运行时我得到了一些奇怪的输出。 I am also trying to do this in the simplest way possible. 我也试图以最简单的方式做到这一点。 How would I go about just printing the word in the suffix array and not all of the extra stuff. 我怎样才能在后缀数组中打印单词而不是所有额外的东西。 I have tried multiple ways to do this and they still show up. 我已经尝试了多种方法来实现这一点,但它们仍然会出现。

    #include <iostream>
    #include <conio.h>
    #include <string>

    using namespace std;

    int main(){

        char word1[80];
        char word2[80];
        char suffix[80];

        cout << "Enter the first word: ";
        cin >> word1;

        cout << "Enter the first word: ";
        cin >> word1;

        int len1 = strlen(word1);
        int len2 = strlen(word2);

        while(len1 > 0 && len2 > 0 && word1[len1] == word2[len2]) {
            int k=0;
            suffix[k]=word1[len1];  

            k++;
            len1--;
            len2--;
        }



        for(int i=strlen(suffix);i>=0; i--){
            cout << suffix[i];
        }



        getch();
        return 0;
    }

Several things: 几件事:

  • You should better use string instead of an array of char . 你应该更好地使用string而不是char数组。 That way, you don't have to worry about memory. 这样,你不必担心记忆。
  • The line int k=0; 线int k=0; should be outside of the while . 应该是外面的while
  • Remember that arrays start at 0, so substract 1 from the length of the words and iterate while len1 >= 0 && len2 >= 0 请记住,数组从0开始,因此从单词长度中减去1并迭代,而len1 >= 0 && len2 >= 0
  • Using strings, you can use the method substr (reference here ). 使用字符串,您可以使用方法substr此处引用)。

Here is a modified version of your code: 以下是您的代码的修改版本:

#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

int main() {
    string word1,word2,suffix;
    cout << "Enter the first word: ";
    cin >> word1;
    cout << "Enter the first word: ";
    cin >> word2;
    int len1 = word1.size()-1;
    int len2 = word2.size()-1;
    int k=0;
    while(len1 >= 0 && len2 >= 0 && word1[len1] == word2[len2]) {
        len1--;
        len2--;
        k++;
    }
    suffix=word1.substr(word1.size()-k,k);
    cout << suffix;
    getch();
    return 0;
}

I always think the "simplest way possible" is to use someone else's work. 我一直认为“最简单的方法”就是利用别人的工作。 Here is one way to write your program that leverages the standard library: 以下是编写利用标准库的程序的一种方法:

#include <algorithm>
#include <string>
#include <iostream>

std::string suffix(const std::string& a, const std::string& b) {
    size_t len = std::min(a.size(), b.size());
    auto its = std::mismatch(a.rbegin(), a.rbegin()+len, b.rbegin());
    return std::string(its.first.base(), a.end());

}

int main () {
    std::cout << suffix("December", "May") << "\n";
    std::cout << suffix("January", "February") << "\n";
    std::cout << suffix("April", "April") << "\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM