POJ 2138 Travel Games
来源:互联网 发布:怎么在淘宝找代销 编辑:程序博客网 时间:2024/05/22 20:06
题目大意:给定一个起始单词,该单词长度为3,再给定一个字典在每一步,在当前单词的任意位置处插入一个字母,使得新得到的单词仍在字典中,下一步以新的单词为起点,继续上述过程,直到当前单词中无法找到合法插入位置为止。
要求输出可达的最长单词。
把每个单词编号,预处理他们之间的到达情况,建图,然后bfs。。
这个做法貌似繁琐了一点,但是我第一感觉就是该这样做,没想太多就敲了。
后来小灯告诉了我一种更简单更快的方法...
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <map>#include <queue>#include <vector>using namespace std;map<string,int> mp;queue<int> q;char str[100],ori[100];char dic[1100][100];int v[1100],len[1100];vector<int> g[1100];bool check(char *s1,char *s2){ int t1,t2; bool flag; flag=true; t1=0; t2=0; while (s1[t1] && s2[t2]) { if (s1[t1] == s2[t2]) { t1++; t2++; } else if (s1[t1] != s2[t2] && flag == true) { t2++; flag=false; } else if (s1[t1] != s2[t2] && flag == false) { return false; } } return true;}int main(){ int n,i,j,t,ans; scanf("%d %s",&n,ori); for (i=0; i<n; i++) { g[i].clear(); scanf("%s",dic[i]); mp[dic[i]]=i; len[i]=strlen(dic[i]); } for (i=0; i<n; i++) { for (j=0; j<n; j++) { if (i == j) continue; if (strlen(dic[i])+1 == strlen(dic[j])) { if (check(dic[i],dic[j])) { g[i].push_back(j); } } } } memset(v,0,sizeof(v)); while (!q.empty()) q.pop(); ans=mp[ori]; q.push(mp[ori]); while (!q.empty()) { t=q.front(); q.pop(); for (i=0; i<g[t].size(); i++) { if (v[g[t][i]] == 0) { v[g[t][i]]=1; q.push(g[t][i]); if (len[g[t][i]] > len[ans]) ans=g[t][i]; } } } printf("%s\n",dic[ans]);}
- poj 2138 Travel Games
- POJ 2138 Travel Games
- POJ 2138 Travel Games 笔记
- poj 2138 Travel Games 搜索+自动机水题
- pku2138 Travel Games (搜索)
- pku2138 Travel Games
- 【POJ2138】【Travel Games】
- POJ-1753 flip games
- The Roatin Games poj
- POJ 3230 Travel
- poj 3230 Travel
- POJ 1487 Single-Player Games
- POJ 2633 Funny Games 笔记
- poj 3230 Travel 简单dp
- [水+dp] poj 3230 Travel
- ACM篇:POJ 3230 -- Travel
- POJ 1310 Nonstop Travel 笔记
- Travel
- 正则表达式之查询——Pattern类
- 字符串全排序问题
- Freemaker <#macro>(宏)的使用
- UDP通信(二)
- UDP通信(一)
- POJ 2138 Travel Games
- TCP通信(二)
- FreeMarker中assign指令的用法
- TCP通信(一)
- POJ 3414(Pots)
- 泛型和TreeSet的应用(二)
- 字母小游戏
- 低年级的计算机系统基础:介绍几本教材及国内外大学使用情况
- 泛型和TreeSet的应用(一)