【HDOJ】1462 Word Crosses

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

  1 /* 1462 */
  2 #include <iostream>
  3 #include <string>
  4 #include <map>
  5 #include <queue>
  6 #include <set>
  7 #include <stack>
  8 #include <vector>
  9 #include <deque>
 10 #include <algorithm>
 11 #include <cstdio>
 12 #include <cmath>
 13 #include <ctime>
 14 #include <cstring>
 15 #include <climits>
 16 #include <cctype>
 17 #include <cassert>
 18 #include <functional>
 19 #include <iterator>
 20 #include <iomanip>
 21 using namespace std;
 22 //#pragma comment(linker,"/STACK:102400000,1024000")
 23 
 24 #define sti                set<int>
 25 #define stpii            set<pair<int, int> >
 26 #define mpii            map<int,int>
 27 #define vi                vector<int>
 28 #define pii                pair<int,int>
 29 #define vpii            vector<pair<int,int> >
 30 #define rep(i, a, n)     for (int i=a;i<n;++i)
 31 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 32 #define clr                clear
 33 #define pb                 push_back
 34 #define mp                 make_pair
 35 #define fir                first
 36 #define sec                second
 37 #define all(x)             (x).begin(),(x).end()
 38 #define SZ(x)             ((int)(x).size())
 39 #define lson            l, mid, rt<<1
 40 #define rson            mid+1, r, rt<<1|1
 41 
 42 const int maxn = 30;
 43 char M[maxn][maxn];
 44 char hs1[maxn], vs1[maxn], hs2[maxn], vs2[maxn];
 45 int ph1, pv1, ph2, pv2;
 46 int hmark[26], vmark[26];
 47 
 48 bool f(char *hs, char *vs, int& ph, int& pv) {
 49     int hlen = strlen(hs);
 50     int vlen = strlen(vs);
 51 
 52     memset(hmark, -1, sizeof(hmark));
 53     memset(vmark, -1, sizeof(vmark));
 54     rep(i, 0, hlen) {
 55         if (hmark[hs[i]-'A'] < 0)
 56             hmark[hs[i]-'A'] = i;
 57     }
 58 
 59     rep(i, 0, vlen) {
 60         if (vmark[vs[i]-'A'] < 0)
 61             vmark[vs[i]-'A'] = i;
 62     }
 63 
 64     rep(i, 0, hlen) {
 65         if (vmark[hs[i]-'A'] >= 0) {
 66             ph = i;
 67             pv = vmark[hs[i]-'A'];
 68             return true;
 69         }
 70     }
 71 
 72     return false;
 73 }
 74 
 75 void solve() {
 76     memset(M, ' ', sizeof(M));
 77     int hlen1 = strlen(hs1), hlen2 = strlen(hs2);
 78     int vlen1 = strlen(vs1), vlen2 = strlen(vs2);
 79 
 80     int c1 = ph1, c2 = ph2+hlen1+3;
 81     int r = max(pv1, pv2);
 82     int i, j, k = 0;
 83 
 84     // vertical
 85     int r1, r2;
 86     int mx = 0;
 87 
 88     if (pv1 >= pv2) {
 89         r1 = 0;
 90         r2 = pv1-pv2;
 91     } else {
 92         r2 = 0;
 93         r1 = pv2-pv1;
 94     }
 95 
 96     for (i=0; i<vlen1; ++i,++r1)
 97         M[r1][c1] = vs1[i];
 98     for (i=0; i<vlen2; ++i,++r2)
 99         M[r2][c2] = vs2[i];
100     mx = max(r1, r2);
101 
102     // horizontal
103     for (i=0; i<hlen1; ++i)
104         M[r][k++] = hs1[i];
105     k += 3;
106     for (i=0; i<hlen2; ++i)
107         M[r][k++] = hs2[i];
108 
109     rep(i, 0, mx) {
110         per(j, 0, maxn) {
111             if (M[i][j] != ' ')
112                 break;
113             M[i][j] = '\0';
114         }
115         puts(M[i]);
116     }
117 }
118 
119 int main() {
120     ios::sync_with_stdio(false);
121     #ifndef ONLINE_JUDGE
122         freopen("data.in", "r", stdin);
123         freopen("data.out", "w", stdout);
124     #endif
125 
126     bool f1, f2;
127     int tt = 0;
128     
129     while (scanf("%s",hs1)!=EOF && hs1[0]!='#') {
130         scanf("%s %s %s", vs1, hs2, vs2);
131         f1 = f(hs1, vs1, ph1, pv1);
132         f2 = f(hs2, vs2, ph2, pv2);
133 
134         if (tt++)
135             putchar('\n');
136         if (f1 && f2) {
137             solve();
138         } else {
139             puts("Unable to make two crosses");
140         }
141     }
142 
143     #ifndef ONLINE_JUDGE
144         printf("time = %d.\n", (int)clock());
145     #endif
146 
147     return 0;
148 }

 

posted on 2015-10-31 00:00  Bombe  阅读(252)  评论(0编辑  收藏  举报

导航