zoukankan      html  css  js  c++  java
  • 提取子串

    /*
    题目:提取子串
    内容:

    【代码填空】(满分12分)

    串“abcba”以字母“c”为中心左右对称;串“abba” 是另一种模式的左右对称。这两种情况我们都称这个串是镜像串。特别地,只含有1个字母的串,可以看成是第一种模式的镜像串。

    一个串可以含有许多镜像子串。我们的目标是求一个串的最大镜像子串(最长的镜像子串),如果有多个最大镜像子串,对称中心靠左的优先选中。例如:“abcdeefghhgfeiieje444k444lmn”的最大镜像子串是:“efghhgfe”

    下面的静态方法实现了该功能,请仔细阅读并分析代码,填写空白处的代码,使得程序的逻辑合理,结果正确。

    // 求最大(长度最大)镜像对称子串
    public static String getMaxMirrorString(String s)
    {
    String max_s = ""; // 所求的最大对称子串

    for(int i=0; i<s.length(); i++)
    {
    // 第一种对称模式
    int step = 1;
    try{
    for(;;)
    {
    if(s.charAt(i-step) != s.charAt(i+step)) break;
    step++;
    }
    }catch(Exception e){}

    String s1 = s.substring(_____________________________); // 填空1


    // 第二种对称模式
    step = 0;
    try{
    for(;;)
    {
    if(_________________________________) break; // 填空2
    step++;
    }
    }catch(Exception e){}

    String s2 = s.substring(i-step+1,i+step+1);


    if(s1.length() > max_s.length()) max_s = s1;
    if(s2.length() > max_s.length()) max_s = s2;
    }

    return max_s;
    }


    【注意】
    只填写缺少的部分,不要抄写已有的代码。
    所填写代码不超过1条语句(句中不会含有分号)
    所填代码长度不超过256个字符。
    答案写在“解答.txt”中,不要写在这里!
    */

     1     // 求最大(长度最大)镜像对称子串
     2 class pro30{
     3     public static String getMaxMirrorString(String s)
     4     {
     5         String max_s = "";  // 所求的最大对称子串
     6 
     7         for(int i=0; i<s.length(); i++)
     8         {
     9             // 第一种对称模式
    10             int step = 1;
    11             try{
    12                 for(;;)
    13                 {
    14                     if(s.charAt(i-step) != s.charAt(i+step)) break;
    15                     step++;
    16                 }
    17             }catch(Exception e){}
    18             
    19             String s1 = s.substring(i - step + 1, i+step);     // 填空1
    20             
    21             
    22             // 第二种对称模式
    23             step = 0;
    24             try{
    25                 for(;;)
    26                 {
    27                     if(s.charAt(i-step) != s.charAt(i+step+1))break;    // 填空2
    28                     step++;
    29                 }
    30             }catch(Exception e){}
    31             
    32             String s2 = s.substring(i-step+1,i+step+1);
    33             
    34             if(s1.length() > max_s.length()) max_s = s1;
    35             if(s2.length() > max_s.length()) max_s = s2;
    36         }
    37         
    38         return max_s;                
    39     }
    40     public static void main (String[] args){
    41         System.out.println(getMaxMirrorString("abcdeefghhgfeiieje444k444lmn"));
    42         System.out.println(getMaxMirrorString("djabcdefedcbalj"));
    43     }
    44 }

    /*
    这个题目,逻辑还是比较简单的,只要耐心一点,带入一些数据,
    比如 i = 5,step = 2,然后两个空应该都可以推测出来,

    这里要注意一下,substring 是求一个字符串的字串,第一个参数是起始位置,第二个参数是结束位置的下一个位置,
    */

  • 相关阅读:
    java——对象学习笔记
    ORACLE中如何查看分区表信息
    postgresql相关系统表查询
    oracle系统表查询
    linux shell常用函数,ps
    文本文件合并
    nginx多版本PHP配置
    webuploader实现文件上传
    open abc.txt: The system cannot find the file specified
    PHP在线批量下载文件
  • 原文地址:https://www.cnblogs.com/wsxjbky/p/3059040.html
Copyright © 2011-2022 走看看