Codeforces #650 A. Short Substrings(思维)

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 
#include 
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

你可能感兴趣的:(Codeforces #650 A. Short Substrings(思维))