Codeforces #650 A. Short Substrings(思维)

本文介绍了一种从Bob构造的特殊字符串b中还原原始字符串a的方法。通过对字符串b中每两个相邻字符构成的子串进行分析,利用特定算法确保了原字符串a的准确还原。文章详细解释了算法思路及其实现代码。

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

A. Short Substrings

题目大意:(文末有原题)

一个字符串s,从s[0]开始到s[len-2],每相邻两个字符(a[0]a[1]、a[1]a[2])是一个子串 然后排列下去;

给出一个字符串,这个字符串是通过这样获得的,求原字符串;

思路:

原函数每两个相邻的都会构成一个子串都会输出,所以原字符串s从s[1]开始,都会存在重复的s[i](除了最后一个),所以,我们只需保证每次都输出两个中的一个就可以得到原字符串,并且要保证能输出最后一个,所以i%2 == 1输出即可;

代码:

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

int main() {
	int t;
	cin >> t;
	while(t--) {
		string s;
		cin >> s;
		int len = s.length() ;
		if(len == 2) {      //如果长度为2,则代表只有这一个子串,所以原字符串长度也是2;
			cout << s << endl;
		}else {
			cout << s[0];
			for(int i = 1; i <= len; i++) {
				if(i % 2)     
					cout << s[i]; 
			}
			
			cout << endl;
		}
	}
	
	return 0;
} 

原题:

题目:

Alice guesses the strings that Bob made for her.

At first, Bob came up with the secret string aa consisting of lowercase English letters. The string aa has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.

Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.

For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".

You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.

输入:

The first line contains a single positive integer tt (1≤t≤1000) — the number of test cases in the test. Then tt test cases follow.

Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2≤|b|≤100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that bb was built according to the algorithm given above.

输出:

Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.

样例:

Input:

4
abbaac
ac
bccddaaf
zzzzzzzzzz

Output:

abac
ac
bcdaf
zzzzzz

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值