#include <string>
#include <vector>
#include <iostream>


std::string CommonPrefix(const std::vector<std::string>& words) {
    std::string result = "";
    int lenght = words.size();
    if (lenght == 1) {
        result = words[0];
    } else {
        for (int i = 0; i < lenght; ++i) {
            if (i == 0) {
                int j = 0;
                while ((j < words[i].size()) && (j < words[i + 1].size()) && (words[i][j] == words[i + 1][j])) {
                    result.push_back(words[i][j]);
                    ++j;
                }
                ++i;
            } else {
                std::string temp = "";
                int t = 0;
                while ((t < words[i].size()) && (t < result.size()) && (words[i][t] == result[t])) {
                    temp.push_back(words[i][t]);
                    ++t;
                }
                result = temp;
            }
        }
    }
    return result;
}



int main() {
    std::vector<std::string> v = {"abcdefg", "abcd"};
    std::cout << CommonPrefix(v) << std::endl;
}