Ultraviolet A Problem Solution: 10150-doublets

Source: Internet
Author: User

This problem can be solved by a BFS on a graph that modeling the doublet relationships. If the pair AAnd BForms a doublet, then there is a edge AAnd BIn 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. Int const wordcount = 25200;
  17. Int const wordsize = 20;
  18. Typedef vector <char *> wordtable;
  19. Struct strcomp
  20. {Bool operator () (char * S1, char * S2) {return strcmp (S1, S2) <0 ;}};
  21. Typedef Map <char *, Int, strcomp> wordmap;
  22. Typedef vector <int> wordgraph;
  23. Bool adjacent (char const * S1, char const * S2)
  24. {
  25. Bool adj = false;
  26. For (INT I = 0; S1 [I]! = '/0'; ++ I ){
  27. If (S1 [I]! = S2 [I]) {
  28. Adj =! Adj;
  29. If (! Adj) break;
  30. }
  31. }
  32. Return adj;
  33. }
  34. Void build_wordgraph (wordtable const & wt, wordgraph & WG)
  35. {
  36. WG. Resize (wt. Size ());
  37. For (INT I = 0; I <wt. Size ()-1; ++ I ){
  38. For (Int J = I + 1; j <wt. Size (); ++ J ){
  39. If (adjacent (wt [I], wt [J]) {
  40. WG [I]. push_back (j );
  41. WG [J]. push_back (I );
  42. }
  43. }
  44. }
  45. }
  46. Bool get_doublets (wordgraph const & WG, int S, int T, list <int> & doublets)
  47. {
  48. Queue <int> q;
  49. Vector <bool> visited (WG. Size (), false );
  50. Vector <int> previous (WG. Size ());
  51. Q. Push (s );
  52. Visited [s] = true;
  53. Bool found = false;
  54. While (! Q. Empty ()){
  55. Int v = Q. Front ();
  56. Q. Pop ();
  57. If (V = T ){
  58. Found = true;
  59. Break;
  60. }
  61. For (INT I = 0, size = WG [v]. Size (); I <size; ++ I ){
  62. Int A = WG [v] [I];
  63. If (! Visited [a]) {
  64. Q. Push ();
  65. Visited [a] = true;
  66. Previous [a] = V;
  67. }
  68. }
  69. }
  70. If (found ){
  71. For (; t! = S; t = previous [T]) doublets. push_front (t );
  72. Doublets. push_front (t );
  73. }
  74. Return found;
  75. }
  76. Int main (INT argc, char * argv [])
  77. {
  78. # Ifndef online_judge
  79. Filebuf In, out;
  80. Cin. rdbuf (in. Open (string (argv [0]) + ". In"). c_str (), ios_base: In ));
  81. Cout. rdbuf (out. Open (string (argv [0]) + ". Out"). c_str (), ios_base: Out ));
  82. # Endif
  83. Char words [wordcount] [wordsize];
  84. Int nwords = 0;
  85. For (; cin. Getline (words [nwords], wordsize) & words [nwords] [0]! = '/0 ';
  86. ++ Nwords ){}
  87. /* Index words by size */
  88. Vector <wordtable> WTS (wordsize );
  89. Vector <wordmap> WMS (wordsize );
  90. For (INT I = 0; I <nwords; ++ I ){
  91. Int size = strlen (words [I]);
  92. WTS [size]. push_back (words [I]);
  93. WMS [size]. insert (make_pair (words [I], WTS [size]. Size ()-1 ));
  94. }
  95. /* We will build wordgraph lazily */
  96. Vector <wordgraph> WGS (wordsize );
  97. Char source [wordsize], target [wordsize];
  98. Int ncases = 0;
  99. While (CIN> source> Target ){
  100. If (ncases ++! = 0) cout <'/N ';
  101. List <int> doublets;
  102. Bool found = false;
  103. Int size = strlen (source );
  104. Wordtable & Wt = WTS [size];
  105. Wordmap & WM = WMS [size];
  106. Wordgraph & WG = WGS [size];
  107. If (size = strlen (target )){
  108. If (WG. Empty () build_wordgraph (wt, WG );
  109. Found = get_doublets (WG, WM [Source], WM [target], doublets );
  110. }
  111. If (found ){
  112. List <int>: iterator first = doublets. Begin (), last = doublets. End ();
  113. For (; first! = Last; ++ first) cout <wt [* First] <'/N ';
  114. } Else {
  115. Cout <"no solution./N ";
  116. }
  117. }
  118. Return 0;
  119. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.