2019 9.15顺丰科技笔试 字符串双节棍

本文介绍了一种用于识别双节棍字符串的算法,双节棍字符串是指形如“aaa...bb...aaa”的字符串,其左右两端为相同字符且数量相等,中间为另一重复的字符串。文章提供了Java实现代码,通过扫描输入字符串,识别并输出符合双节棍字符串特征的中间部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先贴下代码,等有空再把题目补上。

双节棍字符串   :   形如   “aaa....bb....aaa”    左右两端为相同的字符,且数量相等,中间为另一重复的字符串

比如  输入      AAABDDDKDDDL         输出DDDKDDD


import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str=in.nextLine();
        char [] ch=str.toCharArray();
        int one=0;
        Character oneChar=ch[0];
        int two=0;
        Character twoChar=null;
        int three=0;
        Character threeChar=null;
        String result=null;
        int sum=0;
        for (int i = 0; i < ch.length; i++) {
			if(i==0||(ch[i]==oneChar&&ch[i]==ch[i-1]&&oneChar!=threeChar)) {
				one++;
			}else if(twoChar==null) {
				twoChar=ch[i];
				two++;
			}else if(twoChar==ch[i]&&ch[i]==ch[i-1]) {
				two++;
			}else if(threeChar==null) {
				threeChar=ch[i];
				three++;
			}else if(threeChar==ch[i]&&ch[i]==ch[i-1]) {
				three++;
			}else {
				oneChar=twoChar;
				one=two;
				twoChar=threeChar;
				two=three;
				threeChar=ch[i];
				three=1;
			}
			if(one==three&&two<one) {
				if(one*2+two>sum) {
					StringBuilder sb=new StringBuilder();
					for (int j = 0; j < one; j++) {
						sb.append(oneChar);
					}
					for (int j = 0; j < two; j++) {
						sb.append(twoChar);
					}
					for (int j = 0; j < one; j++) {
						sb.append(threeChar);
					}
					sum=one*2+two;
					result=sb.toString();
					oneChar=threeChar;
					one=three;
					twoChar=null;
					two=0;
					threeChar=null;
					three=0;
			}
		}
        } 
        System.out.println(result);
        
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值