zoukankan      html  css  js  c++  java
  • 取字符串前缀

    题目:第一行:输入一个整数n

       接下来n行,输入字符串

       输出n行(字符串前缀)

       例子:

    代码如下:

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args){
            Scanner in=new Scanner(System.in);
            int n=in.nextInt();
            String str[]=new String[n];
            for (int i = 0; i <n ; i++) {
                str[i]=in.next();
            }
            pre(str);
    
        }
        public static void pre(String str[]){
            StringBuilder temp=new StringBuilder();//临时字符串
            String preStr[]=new String[str.length];//将获取的前缀放入此数组
            for (int i = 0; i < str.length; i++) {//此层循环是遍历每个字符串
                temp.append(str[i].charAt(0));
                int n=1;//取下一个字符
                for (int j = 0; j <str.length ; j++) {//此层循环是将str[i]的字符串与其他字符串诼一比较
                    if (j==i){//不与自己比较
                        continue;
                    }
                    while (str[j].startsWith(String.valueOf(temp))) {//当存在此前缀时,temp继续添加下一个字符
                        temp.append(str[i].charAt(n));
                        n++;
                        if (n == str[i].length()) {
                            break;
                        }
                    }
                }
    
                preStr[i]= String.valueOf(temp);
                temp.delete(0, temp.length());
    
            }
    
    
            for (int i = 0; i < str.length; i++) {
                System.out.println(preStr[i]);
            }
        }
    }
  • 相关阅读:
    MVC学习中遇到问题
    静态类和单例模式区别
    类或方法名后加<>
    MVC5入门
    开发BI系统时的需求分析研究
    BI项目需求分析书-模板
    商业智能学习系统
    数据库设计三大范式[转]
    BW对应后台表[转]
    SQL优化方案
  • 原文地址:https://www.cnblogs.com/yeleia/p/9686553.html
Copyright © 2011-2022 走看看