UVA 159 Word Crosses

#include 
#include 
#include 
#include 
using namespace std;
int d[4];
string s[4];
int jud() {
    int a = 0;
    for(int i = 0; i < s[0].size(); i++) {
        for(int j = 0; j < s[1].size(); j++) {
            if(s[0][i] == s[1][j]) {
                d[0] = i;
                d[1] = j;
                a = 1;
                break;
            }
        }
        if(a == 1)
            break;
    }
    if(a == 0)
        return 0;
    a = 0;
    for(int i = 0; i < s[2].size(); i++) {
        for(int j = 0; j < s[3].size(); j++) {
             if(s[2][i] == s[3][j]) {
                 d[2] = i;
                 d[3] = j;
                 a = 1;
                 break;
             }
        }
        if(a == 1)
            break;
    }
    if(a == 0)
        return 0;
    return 1;
}
void pri() {
    int l = s[0].size();
    int h1 = max(d[1], d[3]);
    int h11 = h1 + s[1].size() -d[1];
    int h12 = h1 + s[3].size() - d[3];
    int h = max(h11, h12);
    int h2 = max(d[1], d[3]);
    int o1 = h2 - d[1];
    int o2 = h2 - d[3];
    for(int i = 0; i < h; i++) {
        if(i == h2) {
            cout << s[0] << "   " << s[2] << endl;
            continue;
        }
        if(i - o1 >= 0 && i - o1 1].size()) {
            for(int i = 0; i < d[0]; i++) {
                cout << " ";
            }
            cout << s[1][i-o1];
        }
        else {
            for(int i = 0; i <= d[0]; i++) {
                cout << " ";
            }
        }
        if(i - o2 >= 0 && i - o2 3].size()) {
            for(int i = 0; i < s[0].size() - d[0] + d[2] + 2; i++) {
                cout << " ";
            }
            cout << s[3][i-o2];
        }
        cout << endl;
    }
}
int main() {
    int x = 0;
    while(cin >> s[0]) {
        if(s[0] == "#")
            break;
        if(x) {
            cout << endl;
        }
        x = 1;
        for(int i = 1; i <= 3; i++) {
            cin >> s[i];
        }
        if(!jud()) {
            cout << "Unable to make two crosses" << endl;
        }
        else {
            pri();
        }
    }
    return 0;
}

你可能感兴趣的:(刷题)