当前位置:   article > 正文

【算法】单词接龙_单词接龙算法

单词接龙算法

最近参加校招,看算法。因为DFS考的很多,自己掌握也不是很牢固,就找了一些题。

其中这一道DFS——【单词接龙】,很典型。


大概如下:

    单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现一次两次),在两个单词相连时,其重合部分合为一部分,例如beast和astonish,如果接成一条龙则变为beastonish,另外相邻的两部分不能存在包含关系,例如at和atide间不能相连。

   输入的第一行为一个单独的整数n(n<=20)表示单词数,以下n行每行有一个单词,输入的最后一行为一个单个字符,表示“龙”开头的字母。你可以假定以此字母开头的“龙”一定存在.

   只需输出以此字母开头的最长的“龙”的长度

5

at

touch

cheat

choose

tact

a

 

20    



代码如下:看注释就行

  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;
  6. public class Main {
  7. int n;
  8. List<String> list;
  9. String st;
  10. boolean[] visited;
  11. int[][] edge;
  12. int curSum=0;
  13. String longest = "";
  14. public Main(){
  15. n=8;
  16. list = new LinkedList<String>();
  17. list.add("at");
  18. list.add("touch");
  19. list.add("cheat");
  20. list.add("choose");
  21. list.add("tact");
  22. list.add("about");
  23. list.add("eclipse");
  24. list.add("heightandweight");
  25. st = "a";
  26. visited=new boolean[n];
  27. edge = new int[n][n];
  28. //构造图
  29. for(int i=0;i<n;i++){
  30. for(int j=0;j<n;j++){
  31. boolean hasEdge = false;
  32. //不能和自己相连,且不能存在包含关系
  33. if(i!=j&&!(list.get(i).contains(list.get(j))||list.get(j).contains(list.get(i)))){
  34. int index=0;
  35. //寻找最长后缀
  36. while(!hasEdge&&index < list.get(i).length() ){
  37. if(list.get(j).startsWith(list.get(i).substring(index, list.get(i).length()))){
  38. hasEdge = true;
  39. edge[i][j] = list.get(i).length()-index;
  40. break;
  41. }
  42. index++;
  43. }
  44. }
  45. System.out.print(edge[i][j]+" ");
  46. }
  47. System.out.println();
  48. }
  49. }
  50. public void getSum(){
  51. for(int i=0;i<list.size();i++){
  52. if(list.get(i).startsWith(st)){
  53. int sum=list.get(i).length();
  54. visited[i]=true;
  55. dfs(i,sum,visited,list.get(i));
  56. visited[i]=false;
  57. }
  58. }
  59. System.out.println("the answer is:"+curSum);
  60. System.out.println("the longest dragon is:"+longest);
  61. }
  62. //递归入口
  63. public boolean dfs(int i,int a,boolean[] visited,String curString){
  64. int sum =a;
  65. String s = new String(curString);
  66. System.out.println("before:"+s);
  67. for(int j=0;j<n;j++){
  68. if(edge[i][j]>0){
  69. if(!visited[j]){
  70. sum+=list.get(j).length();
  71. sum-=edge[i][j];
  72. s+=list.get(j).substring(edge[i][j],list.get(j).length());
  73. System.out.println("after:"+s);
  74. visited[j]=true;
  75. if(dfs(j,sum,visited,s)){ //判断为真,表示已经出现一个解,同时当前节点没有与之相邻的下一个节点了,因此要回溯到上一层
  76. if(sum>curSum){ //计算当前单词长度,保留最长
  77. curSum=sum;
  78. longest = s;
  79. System.out.println("sum:"+curSum+" "+"final:"+s);
  80. }
  81. s = s.substring(0, s.length()-list.get(j).length()+edge[i][j]);
  82. System.out.println("recurse:"+s);
  83. visited[j]=false;
  84. }
  85. sum-=list.get(j).length();
  86. sum+=edge[i][j];
  87. }
  88. }
  89. }
  90. return true; //遍历完节点了
  91. }
  92. public static void main(String[] args){
  93. new Main().getSum();
  94. }
  95. }



控制台输出:

  1. 0 1 0 0 1 0 0 0
  2. 0 0 2 2 0 0 0 1
  3. 0 1 0 0 1 0 0 0
  4. 0 0 0 0 0 0 1 0
  5. 0 1 0 0 0 0 0 0
  6. 0 1 0 0 1 0 0 0
  7. 0 0 0 0 0 0 0 0
  8. 0 1 0 0 1 0 0 0
  9. before:at
  10. after:atouch
  11. before:atouch
  12. after:atoucheat
  13. before:atoucheat
  14. after:atoucheatact
  15. before:atoucheatact
  16. sum:12 final:atoucheatact
  17. recurse:atoucheat
  18. recurse:atouch
  19. after:atouchoose
  20. before:atouchoose
  21. after:atouchooseclipse
  22. before:atouchooseclipse
  23. sum:16 final:atouchooseclipse
  24. recurse:atouchoose
  25. recurse:atouch
  26. after:atoucheightandweight
  27. before:atoucheightandweight
  28. after:atoucheightandweightact
  29. before:atoucheightandweightact
  30. sum:23 final:atoucheightandweightact
  31. recurse:atoucheightandweight
  32. recurse:atouch
  33. recurse:at
  34. after:atact
  35. before:atact
  36. after:atactouch
  37. before:atactouch
  38. after:atactoucheat
  39. before:atactoucheat
  40. recurse:atactouch
  41. after:atactouchoose
  42. before:atactouchoose
  43. after:atactouchooseclipse
  44. before:atactouchooseclipse
  45. recurse:atactouchoose
  46. recurse:atactouch
  47. after:atactoucheightandweight
  48. before:atactoucheightandweight
  49. recurse:atactouch
  50. recurse:atact
  51. recurse:at
  52. before:about
  53. after:aboutouch
  54. before:aboutouch
  55. after:aboutoucheat
  56. before:aboutoucheat
  57. after:aboutoucheatact
  58. before:aboutoucheatact
  59. recurse:aboutoucheat
  60. recurse:aboutouch
  61. after:aboutouchoose
  62. before:aboutouchoose
  63. after:aboutouchooseclipse
  64. before:aboutouchooseclipse
  65. recurse:aboutouchoose
  66. recurse:aboutouch
  67. after:aboutoucheightandweight
  68. before:aboutoucheightandweight
  69. after:aboutoucheightandweightact
  70. before:aboutoucheightandweightact
  71. sum:26 final:aboutoucheightandweightact
  72. recurse:aboutoucheightandweight
  73. recurse:aboutouch
  74. recurse:about
  75. after:aboutact
  76. before:aboutact
  77. after:aboutactouch
  78. before:aboutactouch
  79. after:aboutactoucheat
  80. before:aboutactoucheat
  81. recurse:aboutactouch
  82. after:aboutactouchoose
  83. before:aboutactouchoose
  84. after:aboutactouchooseclipse
  85. before:aboutactouchooseclipse
  86. recurse:aboutactouchoose
  87. recurse:aboutactouch
  88. after:aboutactoucheightandweight
  89. before:aboutactoucheightandweight
  90. recurse:aboutactouch
  91. recurse:aboutact
  92. recurse:about
  93. the answer is:26
  94. the longest dragon is:aboutoucheightandweightact



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/718609
推荐阅读
相关标签
  

闽ICP备14008679号