课堂练习----计算最长英语单词链
大家经常玩成语接龙游戏,我们试一试英语的接龙吧:一个文本文件中有N 个不同的英语单词, 我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最多只能用一次。最长的定义是:最多单词数量,和单词中字母的数量无关。
统一输入文件名称:input1.txt, input2.txt
统一输出文件名称:output1.txt,output2.txt
程序需要考虑下列异常状况:
例如,文件不存在,你的程序会崩溃么,还是能优雅地退出并给用户提示信息?
如果文件没有任何单词、只有一个单词、没有可以首尾相连的单词,程序应该如何输出?
如果输入文件有一万个单词,你的程序能多快输出结果?
代码:
wordConnect.java
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.FileReader; 8 import java.io.FileWriter; 9 import java.io.IOException; 10 import java.io.InputStreamReader; 11 import java.io.OutputStreamWriter; 12 import java.io.Writer; 13 import java.text.DecimalFormat; 14 import java.text.NumberFormat; 15 import java.util.ArrayList; 16 import java.util.HashMap; 17 import java.util.Iterator; 18 import java.util.List; 19 import java.util.Scanner; 20 public class WordConnect { 21 static int N=5; 22 static String b[]=new String[500]; 23 public static void main(String[] args) 24 { 25 26 27 String sz=writeFromFile.readTxtFile("input3.txt"); 28 String ltxt=null; 29 if(sz==null) 30 { 31 System.out.println("找不到指定的文件"); 32 } 33 else if(sz=="kong") 34 { 35 System.out.println("文件为空!"); 36 } 37 38 else 39 { 40 System.out.println(ltxt=StatList1(sz)); 41 42 select(b); 43 } 44 45 try { 46 writeFromFile.daochu(ltxt); 47 } catch (IOException e) { 48 // TODO Auto-generated catch block 49 e.printStackTrace(); 50 } 51 52 53 54 } 55 public static int woor(String a) 56 { 57 int n=0; 58 File ctoFile = new File("stopword.txt"); 59 InputStreamReader rdCto; 60 try { 61 rdCto = new InputStreamReader(new FileInputStream(ctoFile)); 62 BufferedReader bfReader = new BufferedReader(rdCto); 63 String txtline = null; 64 try { 65 while ((txtline = bfReader.readLine()) != null) 66 { 67 if(txtline.equals(a)) 68 { 69 n=1; 70 } 71 } 72 bfReader.close(); 73 } catch (IOException e) { 74 // TODO Auto-generated catch block 75 e.printStackTrace(); 76 } 77 78 } catch (FileNotFoundException e) { 79 // TODO Auto-generated catch block 80 e.printStackTrace(); 81 } 82 return n; 83 } 84 public static ArrayList<String> getFiles(String path) { 85 ArrayList<String> files = new ArrayList<String>(); 86 File file = new File(path); 87 File[] tempList = file.listFiles(); 88 for (int i = 0; i < tempList.length; i++) { 89 if (tempList[i].isFile()) { 90 files.add(tempList[i].toString()); 91 } 92 if (tempList[i].isDirectory()) { 93 } 94 } 95 return files; 96 } 97 public static String StatList1(String str) { 98 StringBuffer sb = new StringBuffer(); 99 HashMap<String ,Integer> has = new HashMap<String ,Integer> (); 100 String[] slist = str.split("[^a-zA-Z\']+"); 101 for (int i = 0; i < slist.length; i++) 102 { 103 if (!has.containsKey(slist[i])) 104 { 105 has.put(slist[i], 1); 106 } 107 else 108 { 109 has.put(slist[i],has.get(slist[i])+1 ); 110 } 111 } 112 Iterator<String> iterator = has.keySet().iterator(); 113 String a[]=new String[500]; 114 int s[]=new int[500]; 115 int judge; 116 int n=400; 117 for(int i=0;i<n;i++) 118 { 119 iterator = has.keySet().iterator(); 120 while(iterator.hasNext()) 121 { 122 String word = (String) iterator.next(); 123 if(s[i]<has.get(word)) 124 { 125 s[i]=has.get(word); 126 a[i]=word; 127 b[i]=word; 128 } 129 } 130 judge=woor(a[i]); 131 if(judge==1) 132 { 133 n++; 134 has.remove(a[i]); 135 } 136 else 137 { 138 has.remove(a[i]); 139 } 140 } 141 return sb.toString(); 142 } 143 public static void select(String b[]) 144 { 145 List<Object> list=new ArrayList<>(); 146 int q=0; 147 int n0=0; 148 for(int i=0;i<b.length;i++) 149 { 150 if(b[i]!=null) 151 { 152 n0++; 153 } 154 } 155 if(n0==1) 156 { 157 System.out.println("只有一个单词"); 158 } 159 else 160 { 161 String c="and"; 162 q=c.length(); 163 char n1=c.charAt(0); 164 char n2=c.charAt(q-1); 165 list.add(c); 166 for(int j=0;j<b.length;j++) 167 { 168 String w=b[j]; 169 if(w!=null) 170 { 171 int e=w.length(); 172 char n3=w.charAt(0); 173 char n4=w.charAt(e-1); 174 if(n2==n3) 175 { 176 list.add(w); 177 n1=n3; 178 n2=n4; 179 } 180 } 181 } 182 String l=null; 183 if(list.size()==1) 184 { 185 System.out.println("没有首尾相连"); 186 } 187 else 188 { 189 for(int i=0;i<list.size();i++) 190 { 191 l=l+list.get(i)+"->"; 192 System.out.println(list.get(i)+"->"); 193 } 194 try { 195 daochu(l); 196 } catch (IOException e) { 197 // TODO Auto-generated catch block 198 e.printStackTrace(); 199 } 200 } 201 } 202 } 203 public static void daochu(String a) throws IOException 204 { 205 File file =new File("output1.txt"); 206 Writer out =new FileWriter(file); 207 String data=a; 208 out.write(data); 209 out.close(); 210 } 211 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~