• 计算最长英语单词链


    要求:

    一个文本文件中有N 个不同的英语单词, 我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最多只能用一次。最长的定义是:最多单词数量,和单词中字母的数量无关。

    public class Lan {
            @SuppressWarnings("unused")
                public static void danci(String s) throws IOException {
                       
                        BufferedReader br = new BufferedReader(new FileReader(s));
                        StringBuffer sb = new StringBuffer();
                        String text = null;
                        while ((text = br.readLine()) != null) {
                            sb.append(text);// 将读取出的字符追加到stringbuffer中
                        }
                        br.close(); // 关闭读入流
                        String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写
                        String[] words = str.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词
                        StringBuffer yao = new StringBuffer();
                        String b1=words[0];
                        yao.append(b1);
                        yao.append(" ");
                        
                        String end=b1.substring(b1.length()-1,b1.length());
                        
                       for(int i=1;i<words.length;i++)
                       {  
                        String start=words[i].substring(0,1);
                        if(end.equals(start))
                        {
                            end=words[i].substring(words[i].length()-1,words[i].length());
                            yao.append(words[i]);
                            yao.append("  ");
                        }
                       }
                       
                    
                       File file =new File("output.txt");
                        try {
                             file.createNewFile();
                        } catch (IOException e) {
                           e.printStackTrace();      
                       }
                      
                        try {
                            
                              FileWriter fw =new FileWriter(file);
                              fw.write(yao.toString());
                              fw.flush();
                              fw.close();
                        }
                        catch (IOException e) {
                               e.printStackTrace();      
                           }
                     
                }
    
    
            // 判断文件是否存在
            public static boolean judeFileExists(File file) {
    
                if (file.exists()) {
                    System.out.println("输入文件存在");
                    return true;
                } else {
                    System.out.println("输入文件不存在");
    
                    return false;
                }
            }
            public static void main(String[] args) throws IOException {
                    // TODO 自动生成的方法存根
                    String filename = "input.txt";
                    File a = new File(filename);
                    if (judeFileExists(a)) {
                        danci(filename);
                    } else {
                    }
                }
            }
  • 相关阅读:
    535. Encode and Decode TinyURL 长短URL
    190. Reverse Bits 二进制相反数
    476. Number Complement 二进制中的相反对应数
    598. Range Addition II 矩阵的范围叠加
    507. Perfect Number 因数求和
    asp.net core 系列之用户认证(1)-给项目添加 Identity
    asp.net core 系列之用户认证(authentication)
    asp.net core 系列之webapi集成Dapper的简单操作教程
    asp.net core 系列之webapi集成EFCore的简单操作教程
    asp.net core 系列之中间件基础篇(middleware)
  • 原文地址:https://www.cnblogs.com/xcl666/p/11071370.html
Copyright © 2020-2023  润新知