【codeforces 791C】Bear and Different Names

[Title link] http://codeforces.com/contest/791/problem/C

[title]

Give you n-k+1 limit
Requirement
A [i]. [i] + k-1 contains the same elements, or all of them are different;
Let you output a possible sequence

[Abstract]

Find a section of YES first
(If you can't find them, they all output the same thing.)
Then use this YES as seed.
Suppose that the segment of YES is pos..pos+k-1
Left to right structure
If constructed to the left
That is for (int I = pos-1; I > = 1; I -)
Next, figure out the number for the first position.
If i.i+k-1 is YES
Take the smallest unappeared number in [i.i+k-1];
If i.i+k-1 is NO
Take ans[i]=ans[i+k-1];
such
Ans [i. I + k] are all different.
It won't affect the answer to the left.
To the right
The same is true for YES
The case of NO becomes that ans[i] takes ans[i-k+1].

[Complete code]

#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 rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&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 = 50 + 20;

int n, k, cnt1, po;
int ok[N], ans[N];
int bo[N];
char s[6];
string tar[N];

void in()
{
    rei(n), rei(k);
    rep1(i, 1, n - k + 1)
    {
        scanf("%s", s);
        if (s[0] == 'N')
            ok[i] = 0;
        else
        {
            ok[i] = 1;
            cnt1++;
            po = i;
        }
    }
}

void zhitrue(int pos, int l, int r)
{
    rep1(i, 0, n)
        bo[i] = 0;
    rep1(i, l, r)
        bo[ans[i]]++;
    int x = 0;
    while (bo[x]) x++;
    ans[pos] = x;
}

void zhifalse(int pos, int l, int r,int p)
{
    if (p == 1)
    {
        ans[pos] = ans[r];
    }
    else
    {
        ans[pos] = ans[l];
    }
}

void ga()
{
    if (cnt1 == 0)
    {
        rep1(i, 1, n)
            ans[i] = 1;
    }
    else
    {
        rep1(i, po, po + k - 1)
            zhitrue(i, po, po + k - 1);
        rep2(i, po - 1, 1)
        {
            if (ok[i])
                zhitrue(i, i, i + k - 1);
            else
                zhifalse(i, i, i + k - 1,1);
        }
        rep1(i, po + k, n)
        {
            if (ok[i])
                zhitrue(i, i - k + 1, i);
            else
                zhifalse(i, i - k + 1, i,0);
        }
    }
}

void init()
{
    rep1(i, 1, 26)
    {
        char t = i + 'A' - 1;
        tar[i] = "";
        tar[i] += t;
        tar[i] = t;
    }
    rep1(i, 1, 24)
    {
        char t1 = i + 'A' - 1, t2 = i + 'a' - 1;
        tar[i + 26] = "";
        tar[i + 26] += t1;
        tar[i + 26] += t2;
    }
}

void o()
{
    rep1(i, 1, n)
    {
        cout << tar[ans[i]];
        if (i == n)
            puts("");
        else
            putchar(' ');
    }
}

int main()
{
    //freopen("F:\\rush.txt", "r", stdin);
    init();
    in();
    ga();
    o();
    //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}

Posted by RabidKoala on Mon, 11 Feb 2019 07:36:18 -0800