zoukankan      html  css  js  c++  java
  • 回文字符串问题

    一、判断一个字符串是否为回文字符串(aba)

        public static boolean is_hwzfs(String s, int i, int j) {
            if (i > j) return false;
            for (int k = i; k <= j; ++k) {
                if (s.charAt(k) != s.charAt(j + i - k)) {
                    return false;
                }
            }
            return true;
        }

    二、求一个字符串的最长回文字符串长度并输出

    1)暴力枚举

     1 public static boolean is_hwzfs(char[] s, int i, int j) {
     2         if (i > j) return false;
     3         for (int k = i; k <= j; ++k) {
     4             if (s[k] != s[j + i - k]) {
     5                 return false;
     6             }
     7         }
     8         return true;
     9     }
    10 
    11     public static void main(String[] args) {
    12 
    13         Scanner cin = new Scanner(System.in);
    14         String s = cin.nextLine();
    15 
    16         int len = s.length();
    17         int max = 1;
    18         int x = 0, y = 0;
    19         int[] p = new int[len];
    20         char[] ans = new char[len];
    21         int index = -1;
    22         for (int i = 0; i < len; ++i) {
    23             char ch = s.charAt(i);
    24             if (Character.isLetter(ch)) {
    25                 p[++index] = i;
    26                 ans[index] = Character.toLowerCase(ch);
    27             }
    28         }
    29 
    30 
    31         for (int i = 0; i <= index; ++i) {
    32             for (int j = i; j <= index; ++j) {
    33                 if (is_hwzfs(ans, i, j)) {
    34                     if (max < (j - i + 1)) {
    35                         max = j - i + 1;
    36                         x = p[i];
    37                         y = p[j];
    38                     }
    39                 }
    40             }
    41         }
    42         System.out.println("max= " + max);
    43         for (int i = x; i <= y; ++i) {
    44             System.out.print(s.charAt(i));
    45         }
    46         System.out.println();
    47     }

    2)确定字符串中心,向两边扩展枚举

    1. 长度为奇数:  判断 s[i-j]与s[i+j]  (2*j+1)
    2. 长度为偶数:判断 s[i-j]与 s[i+j+1] (2*(j+1))

    注意 i为字符串中心,j为向两边扩展的长度

    法一:

    法二:

        public static void main(String[] args) {
    
            Scanner cin = new Scanner(System.in);
            String s = cin.nextLine();
    
            int len = s.length();
            int max = 1;
            int x = 0, y = 0;
            int[] p = new int[len];
            char[] ans = new char[len];
            int index = -1;
            for (int i = 0; i < len; ++i) {
                char ch = s.charAt(i);
                if (Character.isLetter(ch)) {
                    p[++index] = i;
                    ans[index] = Character.toLowerCase(ch);
                }
            }
    
            for (int i = 0; i <= index; ++i) {
    
                for (int j = 0; (i - j > 0) && (i + j <= index); ++j) {
                    if (ans[i - j] != ans[i + j]) break;
                    if ((2 * j + 1) > max) {
                        x = p[i - j];
                        y = p[i + j];
                        max = 2 * j + 1;
                    }
                }
    
                for (int j = 0; (i - j > 0) && (i + j + 1 <= index); ++j) {
                    if (ans[i - j] != ans[i + j + 1]) break;
                    if (2 * (j + 1) > max) {
                        x = p[i - j];
                        y = p[i + j + 1];
                        max = 2 * (j + 1);
                    }
                }
            }
    
            System.out.println("max= "+max);
            for (int i = x; i <= y; ++i) {
                System.out.print(s.charAt(i));
            }
            System.out.println();
    
    
        }
  • 相关阅读:
    jsonp跨域
    angular总结控制器的三种主要职责: 为应用中的模型设置初始状态 通过$scope对象把数据模型或函数行为暴露给视图 监视模型的变化,做出相应的动作
    url解析
    waterfall.js
    ES6
    前端代码规范
    秒杀倒计时功能实现
    怎样正确写网站title、keywords、description比较标准。
    CSS3动画
    Python3基础 父,子类普通方法重名 子类方法覆盖父类方法
  • 原文地址:https://www.cnblogs.com/dmir/p/5836876.html
Copyright © 2011-2022 走看看