分割字符串(只包含小写)输入aabbbacc 输出为 aabbba cc 

解决方案 »

  1.   

    没看明白题意,从输入输出来看是c之前和之后分割
    String s = "aabbbacc";
    System.out.println(s.replaceAll("([^c]+)([c]+)", "$1 $2")); //正则不包含c的字符串,和包含c的字符还,用替换的方式在两者之间加个空格
      

  2.   

    全部题目
    给定一个只包含小写字母的字符串,我们通过如下规则分割它:
    1.一个片段中包含的字符不允许出现在其他片段内。
    2.分割的片段数应尽可能的多。
    请实现一个函数,输出分割的片段。
    例如:输入aabbbacceddefgkifk 输出为 aabbba cc edde fgkifk
      

  3.   

    for example
    思路就自己理解一下注释吧public class Sample {
    public static void main(String[] args) {
    try {
    String s = "aabbbacceddefgkifk";
    List<String> list = new ArrayList<>(); //保存结果
    int start = 0, end = 0; //开始,结束位置初始化
    while (true) {
    String ss = s.substring(end, end+1); //取一个字符
    int idx = s.lastIndexOf(ss); //取得当前字符的最后出现位置
    if (idx == end) { //如果最后出现位置和当前字符的位置一样
    end = idx + 1; //因为取出的字符串包括最后一次出现的位置的字符,所以substring的第二个参数要+1
    list.add(s.substring(start, end)); //则保留开始位置到结束位置的字符串
    start = end; //修正开始位置
    } else { //如果最后出现位置和当前字符的位置不一样
    boolean exists = false; //判断从当前字符开始到最后出现位置的字符中间的字符在最后出现位置以后是否出现
    for (int i=end+1; i<idx; i++) {
    String b = s.substring(i, i+1);
    if (s.lastIndexOf(b) > idx) {
    exists = true; //如果出现,则不符合题目条件1,继续找结束位置
    break;
    }
    }
    end = idx+1; //不管出现不出现,都要继续找结束位置,所以修正结束位置
    if (! exists) { //如果不出现,符合条件1
    list.add(s.substring(start, end)); //则保存开始位置到结束位置的字符串
    start = end; //修正开始位置
    }
    }
    if (end >= s.length()) break; //如果结束位置超过字符串长度,则退出循环
    }
    System.out.println(list);
    } catch (Throwable e) {
    e.printStackTrace();
    }
    }
    }
      

  4.   

    请问一下用java代码怎么完成这个动态规划,对动态规划优点不熟王师傅是一名卸货工人,现在有n个货物,由于王师傅一次可以同时卸2个货物,所以决定今天先卸其中的2*m个货物。每次卸货物消耗的体力值计算公式为,假如2个货物的质量分别为x和y,消耗的体力值为(x*y)的4次方,现给出n个货物分别的质量。求王师傅卸完2*m个货物后消耗的体力值是多少。
      

  5.   

    这个题目有意思的,这边有个实现,时间复杂度O(n²),参考下吧:import java.util.*;public class SubStringTest {
        public static void main(String[] args) {
            // 原始字符串
            final String originalStr = "aabbbacceddefgkifk";        // 存放结果
            List<MyStrInfo> resultList = new ArrayList<MyStrInfo>();        // 初始第一个字符串放入结果
            MyStrInfo firstInfo = new MyStrInfo();
            firstInfo.setVal(originalStr.substring(0,1));
            firstInfo.setStartIndex(0);
            resultList.add(firstInfo);        // 从第二个字符开始
            MyStrInfo info;
            String tmpStr;
            for (int i=1;i<originalStr.length();i++){
                tmpStr = originalStr.substring(i,i+1);
                int j=0;
                boolean replaceFlg = false;// 字符串重置标记
                for (;j<resultList.size();j++){
                    info = resultList.get(j);
                    // 如果现有对象包含当前字符,则需要将其合并到新字符串中
                    if(info.getVal().contains(tmpStr)){
                        replaceFlg = true;
                        break;
                    }
                }
                // 若重置标记打开,则重置字符串
                if (replaceFlg){
                    MyStrInfo initInfo = resultList.get(j);
                    // 新字符串起始下标和结束下标
                    int newStart = initInfo.getStartIndex();
                    int newEnd = i+1;
                    // 初始化字符串
                    initInfo.setVal(originalStr.substring(newStart,newEnd));                // 将j之后的对象删除
                    for(int k=resultList.size()-1;k>j;k--){
                        resultList.remove(k);
                    }
                } else {
                    // 新字符串,则新增对象
                    MyStrInfo initInfo = new MyStrInfo();
                    initInfo.setVal(tmpStr);
                    initInfo.setStartIndex(i);
                    resultList.add(initInfo);
                }
            }        // 结果打印
            for(MyStrInfo tmp:resultList){
                System.out.print(tmp.getVal()+" ");
            }
        }
    }class MyStrInfo{
        // 当前字符串
        private String val;    // 当前字符串在原始字符串中的起始下标
        private int startIndex;    public String getVal() {
            return val;
        }    public void setVal(String val) {
            this.val = val;
        }    public int getStartIndex() {
            return startIndex;
        }    public void setStartIndex(int startIndex) {
            this.startIndex = startIndex;
        }
    }