Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
895 B
1
Indexable
Never
#include <iostream>
#include <string>
using namespace std;

string get_common_prefix(string s1, string s2) {
    int len = min(s1.length(), s2.length());
    string prefix = "";
    for (int i = 0; i < len; i++) {
        if (s1[i] == s2[i]) {
            prefix += s1[i];
        } else {
            break;
        }
    }
    return prefix;
}

void find_prefix() {
    string productions[6] = {"x", "xy", "c", "a", "b", "ab"};
    string prefix = productions[0];
    for (int i = 1; i < 6; i++) {
        prefix = get_common_prefix(prefix, productions[i]);
        if (prefix == "") {
            break;
        }
    }
    if (prefix != "") {
        cout << "The CFG contains the same prefix: " << prefix << endl;
    } else {
        cout << "The CFG does not contain the same prefix." << endl;
    }
}

int main() {
    find_prefix();
    return 0;
}