CodeForces B. 3-palindrome

本文由 Ocrosoft 于 2017-05-11 23:20:39 发表

B. 3-palindrome
time limit per test

1 second

memory limit per test

256 megabytes

In the beginning of the new year Keivan decided to reverse his name. He doesn’t like palindromes, so he changed Naviek to Navick.

He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either ‘a‘, ‘b‘ or ‘c‘, with no palindromes of length 3 appearing in the string as a substring. For example, the strings “abc” and “abca” suit him, while the string “aba” doesn’t. He also want the number of letters ‘c‘ in his string to be as little as possible.

Input

The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string.

Output

Print the string that satisfies all the constraints.

If there are multiple answers, print any of them.

Examples
input
2

output
aa

input
3

output
bba

Note

palindrome is a sequence of characters which reads the same backward and forward.

题意:给出一字符串长度N,要求生成一个长度为N的字符串,且这个字符串只由abc组成,并且不能出现回文的长度为3的子串,再者要求c数量最少。

思路:aabbaabb…这种就满足条件,不需要c…

#include <set>
#include <list>
#include <map>
#include <ratio>
#include <stack>
#include <regex>
#include <ctime>
#include <string>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <iostream>
#include <complex>
#include <algorithm>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#define ms(a) memset(a,0,sizeof(a))
#define rep(a,v,b) for(int a=v;a<b;a++)
#define repe(a,v,b) for(int a=v;a<=b;a++)
#define tp(_pair_or_tuple,_index) get<_index>(_pair_or_tuple)
typedef long long LL;
const LL LINF = LLONG_MAX;
const int INF = INT_MAX;
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
const double eps = 1e-7;
const double PI = acos(-1.0);
using namespace std;
template <typename T>
T GCD(T a, T b)
{
	if (!b)return a;
	return GCD(b, a%b);
}
template <typename T>
T LCM(T a, T b)
{
	return a*b / GCD(a, b);
}

int main()
{
	int n; cin >> n;
	string s;
	bool f = false;
	for (int i = 0; i < n / 2; i++)
	{
		s += (f ? "aa" : "bb");
		f = !f;
	}
	if (n % 2)s += (!f ? "b" : "a");
	cout << s << endl;
	return 0;
}

欢迎转载,请保留出处与链接。Ocrosoft » CodeForces B. 3-palindrome

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址