尋找,尋找我的iphone

來源:互聯網
上載者:User

尋找,尋找我的iphone

問題描述
  給定一個長度為n的字串S,還有一個數字L,統計長度大於等於L的出現次數最多的子串(不同的出現可以相交),如果有多個,輸出最長的,如果仍然有多個,輸出第一次出現最早的。
輸入格式
  第一行一個數字L。
  第二行是字串S。
  L大於0,且不超過S的長度。
輸出格式
  一行,題目要求的字串。

  輸入範例1:
  4
  bbaabbaaaaa

  輸出範例1:
  bbaa

  輸入範例2:
  2
  bbaabbaaaaa

  輸出範例2:
  aa
資料規模和約定
  n<=60
  S中所有字元都是小寫英文字母。

  提示
  枚舉所有可能的子串,統計出現次數,找出合格那個

import java.io.*;class Main{    public static void main(String[] args) throws Exception    {        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));        int A =Integer.parseInt(bf.readLine());        String s = bf.readLine().toLowerCase(); //存輸入的數        char c []=s.toCharArray();            //將其轉換為字元數組        int ok_number = 0;                       //記錄每組字串的每個字元是否對應相等        String ok_s0="",ok_s="";             //儲存拆招的最長的子字串        if (A<=60 && A>0 && A<c.length){            char ok_char[] =new char[c.length];     //用來比較沒格子字串用的            int max=1,m=1;            ok_s=""+c[0];                    //初始化            ok_char[A]='\0';            int a=0;int MAX=1;a=A;            for (a=A;a<c.length;a++)           //統計長度大於等於L的出現次數最多的子串            {                for (int i=0;i<c.length-a ;i++ )       //從開始找到左手邊第一個位置                {                    for (int j = i+1;j<=c.length-a ;j++ )    //比較串的左手邊第一個位置                    {                        for (int k=0;k<a ;k++ )                //分別向後比較L                        {                            if (c[i+k]==c[j+k])                 //是否對應相等                            {                                ok_char[ok_number]=c[i+k];                                ok_number++;                            }                        }                        if (ok_number==a)                        {                            m++;                            ok_s0= new String(ok_char,0,a);                        }                        ok_number=0;                    }                    if (max<m)                                  //儲存較大的子串                    {                        max=m;                        s = ok_s0;                    }                    m=1;                }                if (MAX<=max)                                //儲存大於等於L的出現次數最多的子串                {                    MAX=max;                    ok_s=s;                }                max=1;            }            System.out.print(ok_s);        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.