不难的题,不过蛮有意思的dfs
1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <vector> 8 #include <queue> 9 #include <string> 10 #include <cstring> 11 #include <map> 12 #include <stack> 13 #include <set> 14 #define LL long long 15 #define eps 1e-8 16 #define INF 0x3f3f3f3f 17 //#define OPEN_FILE 18 using namespace std; 19 const char step[3][2][6] = { "<ul>", "</ul>", "<ol>", "</ol>", "<li>", "</li>" }; 20 char s[1005][1005]; 21 int m; 22 23 void display(char ch, int f, int pos){ 24 if (pos != 0 && !f){ 25 printf("%s ", step[2][0]); 26 } 27 if (ch == '*'){ 28 if (f){ 29 printf("%s ", step[0][1]); 30 } 31 else{ 32 printf("%s ", step[0][0]); 33 } 34 } 35 else{ 36 if (f){ 37 printf("%s ", step[1][1]); 38 } 39 else{ 40 printf("%s ", step[1][0]); 41 } 42 } 43 if (pos != 0 && f){ 44 printf("%s ", step[2][1]); 45 } 46 } 47 void dfs(int p, int q, int pos){ 48 while (p <= q){ 49 if (s[p][pos] != '#' && s[p][pos] != '*'){ 50 if (pos == 0){ 51 printf("%s ", s[p]); 52 } 53 else{ 54 printf("%s %s %s ", step[2][0], s[p] + pos, step[2][1]); 55 } 56 p++; 57 continue; 58 } 59 int i; 60 bool flag = false; 61 for (i = p + 1; i <= q; i++){ 62 if (s[i][pos] == s[p][pos]){ 63 flag = true; 64 } 65 else{ 66 break; 67 } 68 } 69 i--; 70 if (flag == true){ 71 display(s[p][pos], 0, pos); 72 dfs(p, i, pos + 1); 73 display(s[p][pos], 1, pos); 74 p = i + 1; 75 } 76 else{ 77 if (pos == 0){ 78 printf("%s ", s[p]); 79 } 80 else{ 81 printf("%s %s %s ", step[2][0], s[p] + pos, step[2][1]); 82 } 83 p++; 84 } 85 } 86 } 87 int main() 88 { 89 #ifdef OPEN_FILE 90 freopen("in.txt", "r", stdin); 91 //freopen("out.txt", "w", stdout); 92 #endif // OPEN_FILE 93 m = 1; 94 while (~scanf("%s", s[m])){ 95 m++; 96 } 97 m--; 98 dfs(1, m, 0); 99 //printf("%d ", m); 100 }