您的位置:首页 > 其它

SGU 461 Wiki Lists dfs

2015-07-30 19:20 344 查看
不难的题,不过蛮有意思的dfs

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
//#define OPEN_FILE
using namespace std;
const char step[3][2][6] = { "<ul>", "</ul>", "<ol>", "</ol>", "<li>", "</li>" };
char s[1005][1005];
int m;

void display(char ch, int f, int pos){
if (pos != 0 && !f){
printf("%s\n", step[2][0]);
}
if (ch == '*'){
if (f){
printf("%s\n", step[0][1]);
}
else{
printf("%s\n", step[0][0]);
}
}
else{
if (f){
printf("%s\n", step[1][1]);
}
else{
printf("%s\n", step[1][0]);
}
}
if (pos != 0 && f){
printf("%s\n", step[2][1]);
}
}
void dfs(int p, int q, int pos){
while (p <= q){
if (s[p][pos] != '#' && s[p][pos] != '*'){
if (pos == 0){
printf("%s\n", s[p]);
}
else{
printf("%s\n%s\n%s\n", step[2][0], s[p] + pos, step[2][1]);
}
p++;
continue;
}
int i;
bool flag = false;
for (i = p + 1; i <= q; i++){
if (s[i][pos] == s[p][pos]){
flag = true;
}
else{
break;
}
}
i--;
if (flag == true){
display(s[p][pos], 0, pos);
dfs(p, i, pos + 1);
display(s[p][pos], 1, pos);
p = i + 1;
}
else{
if (pos == 0){
printf("%s\n", s[p]);
}
else{
printf("%s\n%s\n%s\n", step[2][0], s[p] + pos, step[2][1]);
}
p++;
}
}
}
int main()
{
#ifdef OPEN_FILE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // OPEN_FILE
m = 1;
while (~scanf("%s", s[m])){
m++;
}
m--;
dfs(1, m, 0);
//printf("%d\n", m);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: