给你一个字符串例如: "kaaabzzyyyy"
找出字符串中一样的字符连续并且是最多的并算出它的个数
如字符串 "kaaabzzyyyy"  yyyy最多 连续出现了4次
就返回 (y,4)
如给出字符串如 "kaaabbbzzy"
aaa bbb 都出现了3次 知返回先出现的(a,3)
字符串 长度为(1 - 40)
是(a -z)之间的
定义 public String UnitConvert(String aaa)

解决方案 »

  1.   

    从0到length()每次读1个字,初始化变量int a,b,c...z=0,如果读到'a',int a++,依次类推
      

  2.   

    能再说的清楚一点吗?谢谢!
    初始化变量int a,b,c...z=0,如果读到'a',int a++,依次类推
    不是很明白!
    能不能写一点代码 我是低手 高手指教
      

  3.   

    遍历字符串,若a-z之间的记录连续的长度(跟前一个字符比较是否相等)放到一个队列中。
    队列中放的可以是如下结构的对象
    class X {
      char c;
      int  n;
    }
    遍历完之后,在队列中找出n最大的对象,并返回即可。
    可能效率不是很高,但是肯定是可以实现的。
      

  4.   

    嗬嗬,有点意思,java里面能返回(y,4)么?
    学习一下
      

  5.   

    aaayybbyyyy 会得到什么答案?@.@||~
      

  6.   

    String str = "abcccdefffff";
    char[] a1 = str.toCharArray();
    int j1 = 0;
    int j2 = 0;
    int i1 = 0;
    int i2 = 0;
    for (int i = 0; i < a1.length - 1; i++) {
    if (a1[i] == a1[i + 1]) {
    i1 = i; while ((i < a1.length - 1) && (a1[i] == a1[i + 1])) {
    i++;
    }
    i2 = i;
    if ((j2 - j1) < (i2 - i1)) {
    j2 = i2;
    j1 = i1;
    }
    }
    }
    str = new String(a1, j1, j2 - j1 + 1);
    String ret = "("+str.substring(0,1)+","+str.length()+")";
      

  7.   

    刚接触java 
    学习中
      

  8.   

    //在eclipse3.0.1上通过测试
    public class UnitConvert {
    public UnitConvert(){}
    public String getUnitConvert(String code){
    int index = code.length();
    int i=0;
    int j=0;
    String[] result = new String[index];
    int[]  resultNum = new int[index];
    //取出字符串的数,放入result
    for(i=0;i<index;i++){
    result[i] = code.substring(i,i+1);
    //System.out.println("result"+i+"是"+result[i]);
    }
    i = 0;
    //生成resultNum[]
    while(j<index-1){
    if(result[j].equals(result[j+1])){
    resultNum[i]=resultNum[i]+1;
    //System.out.println("resultNum"+i+"是"+resultNum[i]);
    j++;
    }else{
    j++;
    i=j;
    }
    }
    //测试resultNum[i],和result
    /*
    for(i=0;i<index;i++){
    System.out.print("resultNum"+i+"是"+resultNum[i]);
    System.out.println("result"+i+"是"+result[i]);
    }
    */
    //找出resultNum[i]中首先出现最大的数
    int temp=0;
    int bigIndex=0;
    i=0;
    while(i<index-1){
    if(resultNum[i]>temp){
    temp=resultNum[i];
    bigIndex=i;
    i++;
    //System.out.println(temp+"   "+bigIndex);
    }else{
    i++;
    }
    }

    return result[bigIndex]+"最多,出现了"+(temp+1)+"次";
    }
    public static void main(String[] args){
    UnitConvert UC = new UnitConvert();
    String s = "kaaabzzyyyy";
    s = UC.getUnitConvert(s);
    System.out.println(s);
    }
    }
      

  9.   

    csdn的排版不好用啊,麻烦楼主重新排过!
      

  10.   

    public class UnitConvert {
    //空构造
    public UnitConvert(){}
    public static String getUnitConvert(String code){
    int index = code.length();
    int i=0;
    int j=0;
    char[] result = code.toCharArray();
    int[]  resultNum = new int[index];
    i = 0;
    //生成resultNum[]
    while(j<index-1){
    if(result[j]==result[j+1]){
    resultNum[i]=resultNum[i]+1;
    j++;
    }else{
    j++;
    i=j;
    }
    }
    //找出resultNum[i]中首先出现最大的数
    int temp=0;
    int bigIndex=0;
    i=0;
    while(i<index-1){
    if(resultNum[i]>temp){
    temp=resultNum[i];
    bigIndex=i;
    i++;
    }else{
    i++;
    }
    }
    //返回
    return "("+result[bigIndex]+","+(temp+1)+")";
    }
    public static void main(String[] args){
    String s = "kaabbbzzyy";
    s = getUnitConvert(s);
    System.out.println(s);
    }
    }
    改了一下,把一些测试代码去掉了!
    这样好看点!
      

  11.   

    public class UnitConvert {

    public String findUnitConvert(String aaa){
    int index = 0, times = 1, maxTimes = 0;
    char[] ccc = aaa.toCharArray();
    char myChar = 0, maxChar = 0;

    while(index < aaa.length()){
    if(myChar == ccc[index]){
    times++;
    }
    else{
    myChar = ccc[index];
    times = 1;
    }
    if(maxTimes < times){
    maxTimes = times;
    maxChar = myChar;
    }
    index++;
    }
    return "(" + maxChar + ", " + maxTimes + ")";
    }

    public static void main(String[] args){
    UnitConvert uc = new UnitConvert();
    System.out.println(uc.findUnitConvert("kaaabzzyyyy"));
    }
    }
      

  12.   

    public String UnitConvert(String str){
    if(str == null || str.length()==0){
    return null ;
    }
    String result = null ;
    char a = str.charAt(0); //最多的一个字母
    char b ;
    int length = 1 ;
    int maxLength = 1 ;//最多字符的个数
    for(int i=1 ; i<str.length() ;i++){
    b = str.charAt(i);
    if(b == str.charAt(i-1)){
    if(++length > maxLength){
    a = str.charAt(i-1) ;
    maxLength = length ;
    result = "("+ a + "," + maxLength + ")" ;

    }else{
    length = 1 ;
    }
    }
    return result ;
    }
      

  13.   

    int getMaxCount(String strDemo, String strItem) {
        StringTokenizer st = new StringTokenizer(strDemo, strItem ,true);
        int i = 0;
        final int nLen = st.countTokens();
        int nCurrCount = 0;
        int nMax = 0;
        String[] strResult = new String[nLen];
        while (st.hasMoreTokens()) {
    strResult[i] = st.nextToken();
    if (strResult[i].equals(strItem)) {
       ++nCurrCount;
    } else {
        if (nCurrCount>nMax)
        nMax = nCurrCount;
      nCurrCount = 0;
    }
    ++i;
       }
       return nMax;
     }String  strDemo = "kabzzyyaaaayy";
    String strItem = "";
    char [] chArray = strDemo.toCharArray();
    HashMap  hpResult = new HashMap();
    for (int i =0 ;i < chArray.length ; ++i) {
      System.out.println("Begin------------------------------------------------------");
      strItem = ""+ chArray[i];
      if (!hpResult.containsKey(strItem))
        hpResult.put(strItem , ""+getMaxCount(strDemo,strItem));
    }
    //结果存在hpResult中
      

  14.   

    在eclipse上调试通过,测试通过
      

  15.   

    public class Test{
      public static void main(String[] argu) {
        topCharOfString("aabbbbcccdddd");
      }
      
      public static void topCharOfString(String orgin) {
       if(orgin.length()>40) {
        System.out.println("please give me a string whose characters are less than 40!");
        return;
        }
        char[] arrstr = orgin.toCharArray();
        int times = 1;
        char topChar = arrstr[0];
        int topNum = 0;
        for(int i=1; i<arrstr.length; i++) {
        if(arrstr[i]==arrstr[i-1]) {
        times++;
        }else {
        if(times>topNum) {
          topNum = times;
          topChar = arrstr[i-1];
        }
        times = 1;
        }
        }
        System.out.println("("+topNum+",  "+topChar+")");  
      }
    }
      

  16.   

    还算不复杂:
    public class Test{
        
        public static String getResult(String origString){
            char[] stringToChars=origString.toCharArray();
            int maxCount=0,tempCount=0;
            char maxChar=stringToChars[0];
            char tempChar=stringToChars[0];
            for(int i=0;i<origString.length();i++){
                if(tempChar!=stringToChars[i]){
                    tempCount=0;
                    tempChar =stringToChars[i];               
                }
                tempCount++;
                if(tempCount>maxCount){
                    maxCount=tempCount;
                    maxChar=tempChar;                 
                }
            }
            return ("("+maxChar+" , "+maxCount+" )");
        }
        public static void main(String[] args) {
            String testString="kaaabzzyyyy";
            System.out.println(getResult(testString));
            
        }
    }绝对通过测试!
      

  17.   

    to chylwk(沧海一浪) 
    真是英雄所见略同呀!^_^
      

  18.   

    public String getLongestSubstring(String str){
        if (str == null){return "";}
        int len = 1;
        int maxLen = 1;
        String longestSubstring = str.substring(0,1);
        for( int i = 0;i<str.length - 1;i ++){
          if(str.substring(i,i+1).equals(str.substring(i+1,i+2))){
            len ++;
          }else{
            if(len > maxLen){
              maxLen = len;
              len = 1;
              longestSubstring = str.substring(0,i+1);
              str = str.subString(i+1);
            }
          }
        }
        return "(" + longestSubstring + "," + maxLen + ")";}
      

  19.   

    class Result{
    private char c;
    private int t;
    public char getChar(){
    return c;
    }
    public void setChar(char c){
    this.c = c;
    }
    public int getTimes(){
    return t;
    }
    public void setTimes(int t){
    this.t = t;
    }
    }
    public class CSDNExample{
    public Result UnitConvert(String a){
    Result result = new Result();
    char[] str = a.toCharArray();
    int i = a.length();
    int times = 1;
    result.setChar(str[0]);
    result.setTimes(1);
    for(int j = 1; j < i; j++){
    if(str[j]==str[j-1]){
    times++;
    if(times > result.getTimes()){
    result.setTimes(times);
    result.setChar(str[j]);
    }
    }else{
    times = 1;
    }
    }
    return result;
    }
    public static void main(String[] args){
    String s = "kaaabzzyyyy";
    CSDNExample test = new CSDNExample();
    Result result = new Result();
    result = test.UnitConvert(s);
    System.out.println("("+result.getChar()+","+result.getTimes()+")");
    }
    }
      

  20.   

    //不好意思,昨天我的代码有点问题,现在更正如下:(楼主不妨对比一下,也许这样效果更佳)
    //下面的代码已经过测试,未发现问题。问题解决了,楼主给分吧:-)
    public class LongestSubstring{
    private static String getLongestSubstring(String src){
    //为变量赋初值:
    int len = 1;//当前子串的长度
    int maxLen = 1;//当前最长的子串长度
    String longestSubstring = src.substring(0,1);//刚开始时最长的子串(只含首字符)
    int i = 0;

    //核心部分:
    for (; i < src.length() - 1; i++){
    if (src.charAt(i) == src.charAt(i+1)){
    len++;//计算由连续字符组成的子串的长度
    }else{//当前的由连续字符组成的子串已耗尽
    if (len > maxLen){//看它是否有资格申请做当前的最长子串
    //保存当前最长的子串
    maxLen = len;
    longestSubstring = src.substring(0,i + 1);
       }
       //把前面的子串移除,在剩下的串中继续搜索可能存在的最长子串...
    src = src.substring(i + 1);
    len = 1;
    i = -1;//因为一轮循环结束后会进行i++操作,这样可以保证i归0
    }
    }
    if (len > maxLen){//必须检测一下最后处理的子串是否为最长的子串
    //如果去掉该if块,则对abbccc会输出(b,2)而不是正确的值(c,3)
    maxLen = len;
    longestSubstring = src.substring(0,i + 1);
    }
    //核心部分完

    return "(" + longestSubstring.charAt(0) + "," + maxLen + ")";
    }
    public static void main(String [] args){
    //输入值的合法性检测代码从略
    System.out.println("input: " + args[0]);
    System.out.print("output: ");
    System.out.print(LongestSubstring.getLongestSubstring(args[0]));
    }
    }
      

  21.   

    //代码已经过测试,未发现问题。问题解决了,楼主揭贴散分吧:-)
    public class LongestSubstring{
    private static String getLongestSubstring(String src){
    //为变量赋初值:
    int len = 1;//当前子串的长度
    int maxLen = 1;//当前最长的子串长度
    String longestSubstring = src.substring(0,1);//刚开始时最长的子串
                                                          //(只含首字符)
    int i = 0;

    //核心部分:
    for (; i < src.length() - 1; i++){
    if (src.charAt(i) == src.charAt(i+1)){
    len++;//计算由连续字符组成的子串的长度
    }else{//当前的由连续字符组成的子串已耗尽
    if (len > maxLen){//看它是否有资格申请做当前的最长子串
    //保存当前最长的子串
    maxLen = len;
    longestSubstring = src.substring(0,i + 1);
       }
       //把前面的子串移除,在剩下的串中继续搜索可能存在的最长子串...
    src = src.substring(i + 1);
    len = 1;
    i = -1;//因为一轮循环结束后会进行i++操作,
                                      //这样可以保证i归0
    }
    }
    if (len > maxLen){//必须检测一下最后处理的子串是否为最长的子串
    //如果去掉该if块,则对abbccc会输出(b,2)而不是正确的值(c,3)
    maxLen = len;
    longestSubstring = src.substring(0,i + 1);
    }
    //核心部分完

    return "(" + longestSubstring.charAt(0) + "," + maxLen + ")";
    }
    public static void main(String [] args){
    //输入值的合法性检测代码从略
    System.out.println("input: " + args[0]);
    System.out.print("output: ");
    System.out.print(LongestSubstring.getLongestSubstring(args[0]));
    }
    }
      

  22.   

    public class  Test
    {
    public static void main(String[] args) 
    {
    char c1,c2,c_max;
    int sum=1,max=0;

    c_max = 'a';
    String ss  ="22222issssffffffhiiiennnxx";
    c2 = ss.charAt(0);
    for(int i=1;i<ss.length();i++)
    {
    c1 = ss.charAt(i);
    if(c1 == c2)
    {
    sum++;
    }else
    {
    if(sum>max)
    {
    max = sum;
    c_max = c2;
    }
    sum = 1;
    }
    c2 = c1;
    }
    System.out.print("(");
    System.out.print(c_max);
    System.out.print(",");
    System.out.print(max);
    System.out.println(")");
    }
    }
    自我感觉写的比较简单
      

  23.   

    用字母的ascii码做为数组下标,初始都为0,遇到一个就加1,最后输入数组值最大的元素
      

  24.   

    To:chylwk(沧海一浪)
    好像有问题:若testString="kaaabxxxxzzyyyy"。应该返回(x,4)(y,4).可是,你的只能返回(x,4)
      

  25.   

    晕。是我看错了要求。
    对不起,chylwk(沧海一浪)。别骂我。:)
      

  26.   

    嘿嘿..还是 chenxb1980(倾听)的程序思想经典呀.......我得多学习学习....
      

  27.   

    我也参与一个,呵呵,编的不好,大家指教/*
     * 创建日期 2005-6-11
     *
     * TODO 要更改此生成的文件的模板,请转至
     * 窗口 - 首选项 - Java - 代码样式 - 代码模板
     */
    package string;/**
     * @author Michael
     * 
     * TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
     */
    public class FindFrequency {
        public String UnitConvert(String str) {
            StringBuffer resultStr = new StringBuffer("("); //用于返回最终的输出值
            char nextChar = 0;
            int frequency = 1;  //字母的出现频度
            int maxFrequency = 1; //字符的最大频度
            char maxChar = nextChar = str.charAt(0); //第一个字符
            for (int i = 1; i < str.length(); i++) {  //循环
                if (nextChar == str.charAt(i)) { //如果字符连续
                    frequency++;
                  if(maxFrequency<frequency) {  //如果最大频度小于当前频度
                      maxFrequency=minFrequency;
                      maxChar = nextChar; //保存有最大频度的字符
                  }
                } else { //如果下一个字符不连续
                    nextChar = str.charAt(i);  //保存下一个字符,用于和再一个字符比较
                    frequency = 1;
                 }
            }
            resultStr.append(maxChar + "," + maxFrequency + ")");
            return resultStr.toString();
        }    public static void main(String[] a) {
            FindFrequency ff = new FindFrequency();
            String str = ff.UnitConvert("abc");
            System.out.println("the result is :" + str);    }
    }
      

  28.   

    public class Test{
    public static void main(String[] args){
    String str = "abcccdefffff";
    if(args.length>0){str = args[0];}
    int start = 0;
    int maxLength = 1;
    int length = 1;
    //代码核心部分:
    for (int i = 0;i < str.length() - 1;i++){
      if(str.charAt(i) == str.charAt(i + 1)){
        length++;
        if (length > maxLength){
          maxLength = length;
          start = i + 2 - length;
        }
      }else{
        length = 1;
      }
    }//核心部分完
    String retString = "(" + str.charAt(start) + "," + maxLength +")";
    System.out.println(retString);
    }
    }