【codeforces 805B】3palindrome

【题目链接】:http://codeforces.com/contest/805/problem/B

【题意】

让你生成一个只包含a,b,c的字符串;
要求c出现的次数最少,且任意一个 长度为3的子串都不为回文。

【题解】

随便生成一个
abb
然后对于第i位
只要不和第i-2位一样就可以了;
这样就可以只用a和b构成这个字符串了,不包括c,肯定是最优的了.


【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;

int n;

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
    cin >> n;
    if (n==1)
        cout <<"a"<<endl;
    else
        if (n==2)
            cout <<"bb"<<endl;
        else
            if (n>=3)
            {
                string s = "abb";
                rep1(i,3,n-1)
                {
                    if (s[i-2]=='b')
                        s+='a';
                    else
                        s+='b';
                }
                cout << s <<endl;
            }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626340.html