UVa Problem Solution: 10150 - Doublets

本文介绍了一种使用广度优先搜索(BFS)算法解决双字游戏的方法。通过构建一个图来表示单词间的转换关系,并对不同长度的单词分别处理,实现从一个单词到另一个单词的转换路径查找。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


This problem can be solved by a BFS on a graph that modeling the doublet relationships. If the pair of a and b forms a doublet, then there is a edge between a and b in the graph.

You can divide the dictionary into portions by word size, then handle each portion separately. To get a better RT, build each graph lazily only when needed.

Code:
  1. /*************************************************************************
  2.  * Copyright (C) 2008 by liukaipeng                                      *
  3.  * liukaipeng at gmail dot com                                           *
  4.  *************************************************************************/
  5. /* @JUDGE_ID 00000 10150 C++ "Doublets" */
  6. #include <cstring>
  7. #include <iostream>
  8. #include <fstream>
  9. #include <string>
  10. #include <map>
  11. #include <queue>
  12. #include <list>
  13. #include <vector>
  14. #include <map>
  15. using namespace std;
  16.      
  17. int const wordcount = 25200;
  18. int const wordsize = 20;
  19.        
  20. typedef vector<char *> wordtable;
  21. struct strcomp 
  22. bool operator()(char *s1, char *s2) { return strcmp(s1, s2) < 0; } };
  23. typedef map<char *, int, strcomp> wordmap;
  24. typedef vector<vector<int> > wordgraph;
  25. bool adjacent(char const *s1, char const* s2)
  26. {
  27.   bool adj = false;
  28.   for (int i = 0; s1[i] != '/0'; ++i) {
  29.     if (s1[i] != s2[i]) {
  30.       adj = !adj;
  31.       if (!adj) break;
  32.     }
  33.   }
  34.   return adj;
  35. }
  36. void build_wordgraph(wordtable const& wt, wordgraph& wg)
  37. {
  38.   wg.resize(wt.size());
  39.   for (int i = 0; i < wt.size() - 1; ++i) {
  40.     for (int j = i + 1; j < wt.size(); ++j) {
  41.       if (adjacent(wt[i], wt[j])) {
  42.         wg[i].push_back(j);
  43.         wg[j].push_back(i);
  44.       }
  45.     }
  46.   }
  47. }
  48. bool get_doublets(wordgraph const& wg, int s, int t, list<int>& doublets)
  49. {
  50.   queue<int> q;
  51.   vector<bool> visited(wg.size(), false);
  52.   vector<int> previous(wg.size());
  53.   q.push(s);
  54.   visited[s] = true;
  55.   bool found = false;
  56.   while (!q.empty()) {
  57.     int v = q.front();
  58.     q.pop();
  59.     if (v == t) {
  60.       found = true;
  61.       break;
  62.     }
  63.     for (int i = 0, size = wg[v].size(); i < size; ++i) {
  64.       int a = wg[v][i];
  65.       if (!visited[a]) {
  66.         q.push(a);
  67.         visited[a] = true;
  68.         previous[a] = v;
  69.       }
  70.     }
  71.   }
  72.   
  73.   if (found) {
  74.     for (; t != s; t = previous[t]) doublets.push_front(t);
  75.     doublets.push_front(t);
  76.   }
  77.   return found;
  78. }
  79. int main(int argc, char *argv[])
  80. {
  81. #ifndef ONLINE_JUDGE
  82.   filebuf in, out;
  83.   cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
  84.   cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
  85. #endif
  86.   char words[wordcount][wordsize];
  87.   int nwords = 0;
  88.   for (; cin.getline(words[nwords], wordsize) && words[nwords][0] != '/0'
  89.        ++nwords) {}
  90.   /* index words by size */
  91.   vector<wordtable> wts(wordsize);
  92.   vector<wordmap> wms(wordsize);
  93.   for (int i = 0; i < nwords; ++i) {
  94.     int size = strlen(words[i]);
  95.     wts[size].push_back(words[i]);            
  96.     wms[size].insert(make_pair(words[i], wts[size].size() - 1));
  97.   }
  98.   
  99.   /* we will build wordgraph lazily */
  100.   vector<wordgraph> wgs(wordsize);
  101.   char source[wordsize], target[wordsize];
  102.   int ncases = 0;
  103.   while (cin >> source >> target) {
  104.     if (ncases++ != 0) cout << '/n';
  105.     list<int> doublets;
  106.     bool found = false;
  107.     int size = strlen(source);
  108.     wordtable& wt = wts[size];
  109.     wordmap& wm = wms[size];
  110.     wordgraph& wg = wgs[size];
  111.     if (size == strlen(target)) {
  112.       if (wg.empty()) build_wordgraph(wt, wg);
  113.       found = get_doublets(wg, wm[source], wm[target], doublets);
  114.     }
  115.     if (found) {
  116.       list<int>::iterator first = doublets.begin(), last = doublets.end();
  117.       for (; first != last; ++first) cout << wt[*first] << '/n';
  118.     } else {
  119.       cout << "No solution./n";
  120.     }
  121.   }
  122.   return 0;
  123. }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值