UVa Problem Solution: 10150 - Doublets


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. }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园建设方案旨在通过融合先进技术,如物联网、大数据、人工智能等,实现校园的智能化管理与服务。政策的推动和技术的成熟为智慧校园的发展提供了基础。该方案强调了数据的重要性,提出通过数据的整合、开放和共享,构建产学研资用联动的服务体系,以促进校园的精细化治理。 智慧校园的核心建设任务包括数据标准体系和应用标准体系的建设,以及信息化安全与等级保护的实施。方案提出了一站式服务大厅和移动校园的概念,通过整合校内外资源,实现资源共享平台和产教融合就业平台的建设。此外,校园大脑的构建是实现智慧校园的关键,它涉及到数据中心化、数据资产化和数据业务化,以数据驱动业务自动化和智能化。 技术应用方面,方案提出了物联网平台、5G网络、人工智能平台等新技术的融合应用,以打造多场景融合的智慧校园大脑。这包括智慧教室、智慧实验室、智慧图书馆、智慧党建等多领域的智能化应用,旨在提升教学、科研、管理和服务的效率和质量。 在实施层面,智慧校园建设需要统筹规划和分步实施,确保项目的可行性和有效性。方案提出了主题梳理、场景梳理和数据梳理的方法,以及现有技术支持和项目分级的考虑,以指导智慧校园的建设。 最后,智慧校园建设的成功依赖于开放、协同和融合的组织建设。通过战略咨询、分步实施、生态建设和短板补充,可以构建符合学校特色的生态链,实现智慧校园的长远发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值