麻烦的字符串处理
View Code
//zoj1762 //注意题意,是按挑战名单的列,逐列进行从上至下的输入 #include <iostream> #include <string> using namespace std; const int maxn = 35; int n, fact2[11], roundnum, longest[11], linenum, t; string input[maxn / 2][2]; string list[maxn * 4]; char output[maxn][maxn * 8]; int startp[11]; void init() { int i, pos; string st; memset(longest, 0, sizeof(longest)); memset(output, 0, sizeof(output)); getchar(); for (i = 0; i <= 32; i++) { list[i] = ""; } i = 0; while (1) { getline(cin, st); pos = st.find(" "); if (pos == string::npos) { input[i][0] = st; break; } input[i][0] = st.substr(0, pos); st.erase(0, pos + 1); input[i][1] = st; i++; } } void makelist() { int i, j, start, end, l = (n + 1) / 2, pos; list[1] = input[l - 1][0]; i = 0; while (fact2[i] - 1 < n) i++; roundnum = i; pos = l - 1; for (i = 2; i < roundnum; i++) { pos -= fact2[i - 2]; for (j = fact2[i - 1]; j <= fact2[i] - 1; j++) { list[j] = input[pos + (j - fact2[i - 1]) / 2][j % 2]; } } start = fact2[roundnum - 2]; end = fact2[roundnum - 1] - 1; for (i = l - 2 - (fact2[roundnum - 1] - 2) / 2; i >= 0; i--) { for (j = start; j <= end; j++) if (input[i][0] == list[j] || input[i][1] == list[j]) { list[j * 2] = input[i][0]; list[j * 2 + 1] = input[i][1]; break; } } } void makelongest() { int i, j; for (i = 1; i <= roundnum; i++) for (j = fact2[i - 1]; j <= fact2[i] - 1; j++) if (longest[i] < int(list[j].length())) longest[i] = list[j].length(); } void makestartp() { int i; startp[roundnum] = 0; for (i = roundnum - 1; i >= 1; i--) startp[i] = startp[i + 1] + longest[i + 1] + fact2[roundnum - i - 1] + 2; } void drawname(int x,int y, int length, string name) { int l = name.length(), i; output[x][y] = '_'; output[x][y + l + 1] = '_'; for (i = 0; i <= length; i++) if (i < l) output[x][y + 1 + i] = name[i]; else output[x][y + 1 + i] = '_'; } void drawline(int x, int y, int size) { int i, x1 = x, y1 = y; for (i = 0; i < size; i++) { output[x1][y1] = '\\'; x1++; y1++; } y1--; for (i = 0; i < size; i++) { output[x1][y1] = '/'; x1++; y1--; } } void makeoutput() { int i, nowline, j; bool first; linenum = fact2[roundnum] - 1; for (i = roundnum; i >= 1; i--) { nowline = fact2[roundnum - i] - 1; for (j = fact2[i - 1]; j <= fact2[i] - 1; j++, nowline += fact2[roundnum - i + 1]) { if (list[j] != "") { drawname(nowline, startp[i], longest[i], list[j]); if (j % 2 == 0) drawline(nowline + 1, startp[i] + longest[i] + 2, fact2[roundnum - i]); } } } for (i = 0; i < linenum; i++) { j = startp[1]; while (output[i][j] == 0) j--; for (; j >= 0; j--) if (output[i][j] == 0) output[i][j] = ' '; } } void print() { int i, j; for (i = 0; i < linenum; i++) { if (output[i][0] == 0) continue; for (j = 0; output[i][j] != 0; j++) cout << output[i][j]; cout << endl; } } int main() { int i; //freopen("t.txt", "r", stdin); //freopen("y.txt", "w", stdout); fact2[0] = 1; for (i = 1; i <= 10; i++) fact2[i] = fact2[i - 1] * 2; t = 0; while (cin >> n && n != -1) { printf("Tournament %d", ++t); cout << endl; init(); makelist(); makelongest(); makestartp(); makeoutput(); print(); } return 0; }