zoukankan      html  css  js  c++  java
  • 字符串的使用

    Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的密码进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解。比如进行下列变化 ABBA->12ABBA,ABA->ABAKK,123321->51233214 。因为截获的串太长了,而且存在多种可能的情况(abaaab可看作是aba,或baaab的加密形式),Cathcer的工作量实在是太大了,他只能向电脑高手求助,你能帮Catcher找出最长的有效密码串吗?

    输入描述:
    输入一个字符串

    输出描述:
    返回有效密码串的最大长度

    示例1
    输入
    ABBA
    输出
    4

    解析:把输入的字符串分为2种情况处理,偶数个的时候第一次处理是挨着的,奇数个的时候第一次处理是中间隔一个的,然后循环向前。

    代码:

    package com.dragon.school.daniel.controller;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class Test4444 {
        public static void main(String[] args) throws Exception {
    
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String line;
            while ((line = br.readLine()) != null) {
                int max = 1;
                char[] xx = line.toCharArray();
                int len = xx.length;
                if (len == 0) {
                    System.out.println(0);
                } else {
                    for (int i = 1; i < len - max / 2; i++) {
                        System.out.println("i<:"+(len - max / 2));
                        int low = i - 1, high = i;
                        while (low >= 0 && high < len && xx[low] == xx[high]) {
                            low--;
                            high++;
                            System.out.println("A:"+low+","+high);
                        }
                        if (high - low - 1 > max) {
                            max = high - low - 1;
                        }
    
                        low = i - 1;
                        high = i + 1;
                        while (low >= 0 && high < len && xx[low] == xx[high]) {
                            low--;
                            high++;
                            System.out.println("B:"+low+","+high);
                        }
                        if (high - low - 1 > max) {
                            max = high - low - 1;
                        }
                    }
                }
                System.out.println(max);
            }
    
        }
    }
    
  • 相关阅读:
    1014 Waiting in Line (30)(30 point(s))
    1013 Battle Over Cities (25)(25 point(s))
    1012 The Best Rank (25)(25 point(s))
    1011 World Cup Betting (20)(20 point(s))
    1010 Radix (25)(25 point(s))
    1009 Product of Polynomials (25)(25 point(s))
    1008 Elevator (20)(20 point(s))
    1007 Maximum Subsequence Sum (25)(25 point(s))
    1006 Sign In and Sign Out (25)(25 point(s))
    1005 Spell It Right (20)(20 point(s))
  • 原文地址:https://www.cnblogs.com/InternetJava/p/12543216.html
Copyright © 2011-2022 走看看