poj 2138 DFS
来源:互联网 发布:神结局电影 知乎 编辑:程序博客网 时间:2024/06/05 16:34
首先预处理,哪些字符串是可以互相到达的
还有一点要注意的是,如果一个都找不出来,就输出原单词
AC代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct Edge{ int to, next;};char s[1100][100];int N;Edge edge[400000];int head[1100], tot;int dp[1100];int anspos, ans;void add_edge( int a, int b ){ edge[tot].to = b; edge[tot].next = head[a]; head[a] = tot++;}bool judge( char a[], char b[] ){ if( strlen( a ) != strlen( b ) - 1 ){ return false; } int i, j; i = j = 0; while( a[i] && b[j] ){ if( a[i] == b[j] ){ i++; j++; }else{ j++; } if( j - i > 1 ){ break; } } if( j - i > 1 ){ return false; } if( (unsigned)i == strlen( a ) && (unsigned)j == strlen( b ) ){ return true; } if( (unsigned)i == strlen( a ) && (unsigned)j == strlen( b ) - 1 ){ return true; } return false;}int DFS( int pos, int deep ){ if( deep > ans ){ anspos = pos; ans = deep; } for( int i = head[pos]; i != -1; i = edge[i].next ){ DFS( edge[i].to, deep + 1 ); } return 0;}int main(){ while( scanf( "%d%s", &N, s[0] ) != EOF ){ tot = 0; memset( head, -1, sizeof( head ) ); for( int i = 1; i <= N; i++ ){ scanf( "%s", s[i] ); } for( int i = 0; i <= N; i++ ){ for( int j = 1; j <= N; j++ ){ if( i != j ){ if( judge( s[i], s[j] ) ){ add_edge( i, j ); } } } } anspos = 0; ans = 0; DFS( 0, 0 ); if( ans == 0 ){ cout << s[0] << endl; }else{ cout << s[anspos] << endl; } } return 0;}
0 0
- poj 2138 DFS
- DFS POJ
- POJ DFS
- poj 3083 DFS+DFS+BFS
- POJ 1753 ID+DFS
- poj 1950 数字dfs
- poj 1816(trie+dfs)
- POJ 3107 Godfather(DFS)
- poj 3620(DFS)
- poj 1020 DFS
- poj 2362Square(DFS)
- POJ 2676 Sudoku dfs
- POJ 2357 Labyrinth [DFS]
- poj 1011 dfs
- poj 1190 dfs(生日蛋糕)
- poj 1154 dfs
- poj 2192 DFS+剪枝
- 【DFS】POJ 2044
- java 接口相关
- 安装arm-linux-gcc
- 使用 Pentaho data-integration (Kettle) 进行数据转换出现中文乱码时的解决办法
- 手机系统崩溃丢失数据怎么恢复
- 画1像素红色的描边
- poj 2138 DFS
- centos源码安装subversion
- pcDuino支持的scratch编程(三)用角色控制灯
- android学习总结(一)
- 用visio制作用户界面原型(wireframe/prototype)教程
- SDUT2140_图结构练习——判断给定图是否存在合法拓扑序列(邻接表)
- 快下班了,亲们,再见
- html编写问题总结
- autotrace 和explain plan for可能导致执行计划错误