ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

oj-和最大的连续降序字符-java

2021-03-13 15:34:01  阅读:263  来源: 互联网

标签:java String int 降序 charSet maxString length string oj


Description

Archana is very fond of strings. She likes to solve many questions related to strings. She comes across a problem which she is unable to solve. Help her to solve. The problem is as follows: Given is a string of length L. Her task is to find the longest string from the given string with characters arranged in descending order of their ASCII code and in arithmetic progression. She wants the common difference should be as low as possible(at least 1) and the characters of the string to be of higher ASCII value.

Input

The first line of input contains an integer T denoting the number of test cases. Each test contains a string s of lengthL.

1<= T <= 100

3<= L <=1000

A<=s[i]<=Z

The string contains minimum three different characters.

Output

For each test case print the longest string.Case 1:Two strings of maximum length are possible- “CBA” and “RPQ”. But he wants the string to be of higher ASCII value therefore, the output is “RPQ”.Case 2:The String of maximum length is “JGDA”.

Sample Input 1

2
ABCPQR
ADGJPRT

Sample Output 1

RQP
JGDA

Code

package org.alphacat.third;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class DescendingOrder {

    private static final int ALPHABET_SIZE = 26;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < t; i++) {
            String s = scanner.nextLine();
            String res = problem(s);
            System.out.println(res);
        }

    }

    private static String problem(String s) {
        boolean[] charSet = getCharSet(s);
        int maxLen = 0;
        String maxString = "";
        for (int i = 0; i < charSet.length; i++) {

            if (!charSet[i]) {
                continue;
            }

            for (int step = 1; i + step < charSet.length; step++) {
                int len = 0;
                StringBuilder sb = new StringBuilder();
                for (int j = i; j < charSet.length; j += step) {
                    if (!charSet[j]) {
                        break;
                    }
                    ++len;
                    sb.insert(0, (char) (j + 'A'));
                }

                if (len > maxLen) {
                    maxLen = len;
                    maxString = sb.toString();
                } else if (len == maxLen && sb.charAt(0) > maxString.charAt(0)) {
                    maxString = sb.toString();
                }
            }

        }
        return maxString;
    }

    private static boolean[] getCharSet(String s) {
        boolean[] alphabet = new boolean[ALPHABET_SIZE];
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            alphabet[c - 'A'] = true;
        }
        return alphabet;
    }
}

标签:java,String,int,降序,charSet,maxString,length,string,oj
来源: https://blog.csdn.net/m0_37302219/article/details/114748307

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有