uva 10562 DFS
2015-08-08 10:36
274 查看
#include<bits/stdc++.h> using namespace std; const int maxn=210; int n; char buf[maxn][maxn]; void dfs(int r,int c) { cout<<buf[r][c]<<'('; if(r+1<n&&buf[r+1][c]=='|') { int i=c; while(i-1>=0&&buf[r+2][i-1]=='-') i--; while(buf[r+2][i]=='-'&&buf[r+3][i]!='\0') {if(!isspace(buf[r+3][i])) dfs(r+3,i);i++;} } cout<<')'; } void solve() { n=0; for(;;) { fgets(buf ,maxn,stdin); if(buf [0]=='#') break; else n++; } cout<<'('; if(n) { for(int i=0;i<strlen(buf[0]);i++) if(buf[0][i]!=' ') {dfs(0,i);break;} } cout<<')'<<endl; } int main() { int T; fgets(buf[0],maxn,stdin); sscanf(buf[0],"%d",&T); while(T--) solve(); return 0; }
就是刘汝佳树上的代码
他用了比较少见的fgets和sscanf
fgets 多了一个参数,file * ,填上stdin就和gets相仿了
sscanf 从字符串中读取字符,第一个参数填上字符串指针就可以了
这道题的dfs很简单,就是不断地对字符往下搜索,往下时把前括号打上,退回时把后括号打上 ,就可以了
相关文章推荐
- HDU 5360 Hiking
- php preg_match 过滤字符
- The Blocks Problem
- C#委托的介绍(delegate、Action、Func、predicate)
- VIM命令合集
- Android之——iptables常用命令
- 《算法导论》——数论
- 南邮 OJ 2049 硬币正面数量
- 2251POJ
- 杭电(hdu)ACM 4548 美素数
- arduino 用电位器调节LED闪烁频率
- 判断Sql Server2008中ntext不为空
- 树的实现
- Linux在应用层通过mmap映射后,读写寄存器
- cocos2dx-基本动画制作
- 编码剖析Spring依赖注入的原理
- 利用 Console 来学习、调试JavaScript
- 南邮 OJ 2048 长方体边长和
- CentOS开启FTP及配置用户
- BAT命令大全