最长公共字符串后缀

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        String[] strings = str.split(",");
        int n = strings.length;
        int j = 0;
        String s = strings[j];
        s = new StringBuffer(s).reverse().toString();
        n--;
        for (int i = 0; i < n; i++) {
            String t = strings[++j];
            t = new StringBuffer(t).reverse().toString();
            s = common(s, t);
        }
        s = new StringBuffer(s).reverse().toString();
        System.out.println(s);
    }
    public static String common(String s,String t) {
        int most = 0;
        int len = Math.min(s.length(), t.length());
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) == t.charAt(i)) {
                most++;
            }else {
                break;
            }
        }
        return s.substring(0, most);
    }

C++版本

#include<bits/stdc++.h>
using namespace std;
string common(string s,string t) {
    int most = 0;
    int len = min(s.length(), t.length());
    for (int i = 0; i < len; i++) {
        if (s[i] == t[i])
            most++;
        else
            break;
    }
    return s.substr(0, most);
}
int main() {
    int n;
    while (cin >> n) {
        if (!n) break;
        string s;
        cin >> s;
        reverse(s.begin(), s.end());
        n--;
        for (int i = 0; i < n; i++) {
            string t;
            cin >> t;
            reverse(t.begin(), t.end());
            s = common(s, t);
        }
        reverse(s.begin(), s.end());
        cout << s << endl;
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
string common(string s,string t) {
   
    int most = 0;
    int len = min(s.length(), t.length());
    for (int i = 0; i < len; i++) {
   
        if (s[i] == t[i])
            most++;
        else
            break;
    }
    return s.substr(0, most);
}
int main() {
   
    int n;
    while (cin >> n) {
   
        if (!n) break;
        string s;
        cin >> s;
        reverse(s.begin(), s.end());
        n--;
        for (int i = 0; i < n; i++) {
   
            string t;
            cin >> t;
            reverse(t.begin(), t.end());
            s = common(s, t);
        }
        reverse(s.begin(), s.end());
        cout << s << endl;
    }
    return 0;
}