zoukankan      html  css  js  c++  java
  • 关于计算最长英语单词链的问题

    要求:

    例如, 文件里有:

    Apple

    Zoo

    Elephant

    Under

    Fox

    Dog

    Moon

    Leaf

    Tree

    最长的相连英语单词串为: apple - elephant – tree,

    输出到文件里面,是这样的:

    Apple Elephant Tree

    package test_use_p;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    
    public class Action {
        public static void main(String[] args) throws IOException {
            String[] st = new String[20];
            String[] sf = new String[20];
            String[] ss = new String[20];
            
            String url = "input.txt";
            
            st = readWordT(url);
            sf = readWordF(url);
            ss = readWord(url);
            
            //BufferedWriter out = new BufferedWriter(new FileWriter("output.txt"));
            FileWriter writer = new FileWriter("output.txt");
            BufferedWriter out = new BufferedWriter(writer);
    
            int t = 0;
            int f = 0;
            int temp = 0;
            try {
                for(f = temp;f<20;f++) {
                    for(t = temp;t<20;t++) {
                        if( sf[f].equals(st[t]) ) {
                            System.out.println(ss[++f]);                        
                            out.write(ss[++f]);
                        }
                    }
                }
            }
            catch (Exception e){
                
            }
            
        }
        
        
        
        
        public static void read(int temp,String[] st,String[] sf,String[] ss) {
            int t = 0;
            int f = 0;
            
            for(f = temp;f<20;f++) {
                //System.out.println("fffffffffffffffff"+f);
                for(t = temp;t<20;t++) {
                    //System.out.println("ttttttttttttttttttttt"+t);
                    if( sf[f].equals(st[t]) ) {
                        System.out.println(ss[++f]);
                        temp = t;
                        
                    }
                }
            }
        }
        
        
        @SuppressWarnings("resource")
        public static String[] readWordT(String url) throws IOException {
            String[] s = new String[20];
            int i = 0;
            
            
            BufferedReader br = new BufferedReader(new FileReader(url));
            BufferedReader brspace = new BufferedReader(new FileReader(url));
            int tempc, tempspace;//记录是否为字母或空格
            char c1, c2;//分别记录第一个单词的末尾,第二个单词的开头
            String s1, s2;
            
            
            brspace.read();//多读到下一个
            while( (tempc = br.read()) != -1) { 
                tempspace = brspace.read();
                
                if( (tempc >= 97 && tempc <= 122) && tempspace == 32) {//尾字母
                    c1 = (char)tempc;
                    s1 = String.valueOf(c1);
                    //System.out.println(c1);
                }
                
                if(tempc == 32 && (tempspace >= 97 && tempspace <= 122) ) {//头字母
                    c2 = (char)tempspace;
                    s2 = String.valueOf(c2);
                    //System.out.println(c2);
                    
                    s[i] = s2;//记录到头字符串数组中
                    i++;
                }
                
            }
            
            
            return s;        
        }
        
        
        @SuppressWarnings("resource")
        public static String[] readWordF(String url) throws IOException {
            String[] s = new String[20];
            int i = 0;
            
            
            BufferedReader br = new BufferedReader(new FileReader(url));
            BufferedReader brspace = new BufferedReader(new FileReader(url));
            int tempc, tempspace;//记录是否为字母或空格
            char c1, c2;//分别记录第一个单词的末尾,第二个单词的开头
            String s1, s2;
            
            
            brspace.read();//多读到下一个
            while( (tempc = br.read()) != -1) { 
                tempspace = brspace.read();
                
                
                if( (tempc >= 97 && tempc <= 122) && tempspace == 32) {//尾字母
                    c1 = (char)tempc;
                    s1 = String.valueOf(c1);
                    //System.out.println(c1);
                    
                    
                    s[i] = s1;//记录到末尾字符串数组中
                    i++;
                }
                
                if(tempc == 32 && (tempspace >= 97 && tempspace <= 122) ) {//头字母
                    c2 = (char)tempspace;
                    s2 = String.valueOf(c2);
                    //System.out.println(c2);             
                }
                
            }
            
            
            return s;        
        }
        
        
        @SuppressWarnings("resource")
        public static String[] readWord(String url) throws IOException {
            String[] s = new String[20];
            int i = 0;    
            
            BufferedReader br = new BufferedReader(new FileReader(url));
            
            int tempc;//记录是否为字母或空格
            char c1;//分别记录第一个单词的末尾,第二个单词的开头
            String s1;
            String ss = "";
            
            while( (tempc = br.read()) != -1) {   
                if((tempc >= 97 && tempc <= 122)) {
                    c1 = (char)tempc;
                    s1 = String.valueOf(c1);                              
                    ss = ss.concat(s1);                
                } 
                else if(tempc == 32) {
                    
                    s[i] = ss;
                    i++;
                    //System.out.println(ss);
                    ss = "";
                }
            }    
            return s;        
        }
        
        
    }
  • 相关阅读:
    @WebFilter注解
    Value '0000-00-00' can not be represented as java.sql.Date解决办法
    项目配置 xml文件时 报错提示(The reference to entity "useSSL" must end with the ';' delimiter.)
    eclipse中取消自动生成的TODO Auto-generated method stub
    eclipse中把选中的代码全部变成大写或者小写的快捷键
    在浏览器访问Tomcat的时候报错java.lang.IllegalArgumentException: Control character in cookie value or attribute.
    Tomcat报错,内存溢出的错误Exception in thread "http-bio-8080-exec-13" java.lang.OutOfMemoryError: PermGen space
    java.io.Serializable的作用
    idea快捷键总结
    MYSQL分页limit速度太慢优化方法
  • 原文地址:https://www.cnblogs.com/leity/p/11008611.html
Copyright © 2011-2022 走看看