【HDOJ】1462 Word Crosses

时间:2023-03-08 17:15:39

字符串水题,这么做可能比较巧妙。

 /* 1462 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
char M[maxn][maxn];
char hs1[maxn], vs1[maxn], hs2[maxn], vs2[maxn];
int ph1, pv1, ph2, pv2;
int hmark[], vmark[]; bool f(char *hs, char *vs, int& ph, int& pv) {
int hlen = strlen(hs);
int vlen = strlen(vs); memset(hmark, -, sizeof(hmark));
memset(vmark, -, sizeof(vmark));
rep(i, , hlen) {
if (hmark[hs[i]-'A'] < )
hmark[hs[i]-'A'] = i;
} rep(i, , vlen) {
if (vmark[vs[i]-'A'] < )
vmark[vs[i]-'A'] = i;
} rep(i, , hlen) {
if (vmark[hs[i]-'A'] >= ) {
ph = i;
pv = vmark[hs[i]-'A'];
return true;
}
} return false;
} void solve() {
memset(M, ' ', sizeof(M));
int hlen1 = strlen(hs1), hlen2 = strlen(hs2);
int vlen1 = strlen(vs1), vlen2 = strlen(vs2); int c1 = ph1, c2 = ph2+hlen1+;
int r = max(pv1, pv2);
int i, j, k = ; // vertical
int r1, r2;
int mx = ; if (pv1 >= pv2) {
r1 = ;
r2 = pv1-pv2;
} else {
r2 = ;
r1 = pv2-pv1;
} for (i=; i<vlen1; ++i,++r1)
M[r1][c1] = vs1[i];
for (i=; i<vlen2; ++i,++r2)
M[r2][c2] = vs2[i];
mx = max(r1, r2); // horizontal
for (i=; i<hlen1; ++i)
M[r][k++] = hs1[i];
k += ;
for (i=; i<hlen2; ++i)
M[r][k++] = hs2[i]; rep(i, , mx) {
per(j, , maxn) {
if (M[i][j] != ' ')
break;
M[i][j] = '\0';
}
puts(M[i]);
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif bool f1, f2;
int tt = ; while (scanf("%s",hs1)!=EOF && hs1[]!='#') {
scanf("%s %s %s", vs1, hs2, vs2);
f1 = f(hs1, vs1, ph1, pv1);
f2 = f(hs2, vs2, ph2, pv2); if (tt++)
putchar('\n');
if (f1 && f2) {
solve();
} else {
puts("Unable to make two crosses");
}
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}