双截棍字符串

本文解析了一道顺丰笔试中的编程题,题目要求寻找输入字符串中符合特定模式的最长子串,即所谓的“双截棍字符串”。通过分析给出的C++代码,我们可以看到作者使用了循环和条件判断来识别这种模式,并记录下符合条件的最长子串。

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

顺丰笔试第一道编程题,求“双截棍字符串”

#include<iostream>
#include<string>
using namespace std;

int main(){
	string str;
	cin >> str;

	int len1 = 1, len2 = 1, len3 = 1, len;
	int begin = 0, max = 0, temp;
	for (int i = 0; i<str.length() - 1; i){
		len1 = 1, len2 = 1, len3 = 1;
		if (str[i + 1] == str[i]){

			temp = i;
			while (str[i + 1] == str[i]){
				len1++;
				i++;
			}

			if (i == str.length()-1)
				break;
			else
				i++;
			while (str[i + 1] == str[i]){
				len2++;
				i++;
			}
			if (i == str.length()-1)
				break;
			else
				i++;
			while (str[i + 1] == str[i]){
				len3++;
				i++;
			}
			len = len1 + len2 + len3;
			if (len1 == len3 && len1 > len2 && len > max){
				begin = temp;
				max = len;
				i = temp + len1 + len2;
			}
			else
				i = temp + len1;
		}
		else
			i++;
	}
	if (max == 0)
		cout << "NULL" << endl;
	else{
		cout << str.substr(begin, max) << endl;
	}
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值