AcWing第12场周赛-2021/8/14

Posted 冯大善人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AcWing第12场周赛-2021/8/14相关的知识,希望对你有一定的参考价值。

题目链接 AcWing 3803. 数组去重

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>

using namespace std;

const int N = 60, M = 1010;

int n;
int a[N];
int st[M];
stack<int> w;

int main()
{
    int T;
    cin >> T;
    while (T -- )
    {
        memset(st, 0, sizeof st);
        cin >> n;
        for (int i = 1; i <= n; i ++ ) cin >> a[i], st[a[i]] ++;
        for (int i = n; i >= 1; i -- )
        {
            if(st[a[i]])
            {
                w.push(a[i]);
                st[a[i]] = false;
            }
        }
        cout << w.size() << endl;
        while(w.size())
        {
            cout << w.top() << \' \';
            w.pop();
        }
        cout << endl;
    }
    return 0;
}

AcWing 3804. 构造字符串

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10, M = 30;;

int T, n, k;
char s[N];
bool st[M];
char a[N];
char ans[2];
char cos[2];

void f(char cos[])
{
    int a = cos[0] - \'a\';
    int b = cos[1] - \'a\';
    bool flag = false;  //能不能找到比b大的最小的字母
    for (int i = b + 1; i < 26; i ++ )
    {
        if(st[i])
        {
            flag = true;
            b = i;  //更新b;
            break;
        }
    }
    if(!flag)  //如果没找到
    {
        for(int i = a + 1; i < 26; i ++)
        {
            if(st[i])
            {
                a = i;
                break;
            }
        }
        for(int i = 0; i < 26; i ++)
        {
            if(st[i])
            {
                b = i;
                break;
            }
        }
    }
    ans[0] = a + \'a\';
    ans[1] = b + \'a\';
    
}

int main()
{
    cin >> T;
    while (T -- )
    {
        memset(st, false, sizeof st);
        
        scanf("%d%d%s", &n, &k, s);
        for(int i = 0; i < n; i ++) st[s[i] - \'a\'] = true;

        if( n < k )
        {
            char c;  //找最小的
            for (int i = 0; i < 26; i ++ )
            {
                if(st[i])
                {
                    c = i + \'a\';
                    break;
                }
            }
            for (int i = 0; i < n; i ++ ) a[i] = s[i];
            for (int i = n; i < k; i ++) a[i] = c;
        }
        
        else
        {
            if(k == 1)
            {
                char c;
                int idx = 0;
                int x = s[0] - \'a\';
                for (int i = x + 1; i < 26; i ++ )
                {
                    if(st[i])
                    {
                        
                        c = i + \'a\';
                        break;
                    }
                }
                a[0] = c;
            }
            else
            {
                //找最大的
                char m;
                for (int i = 25; i >= 0; i -- )
                {
                    if(st[i])
                    {
                        m = i + \'a\';
                        break;
                    }
                }
                // 找最小的
                char h;
                for (int i = 0; i < 26; i ++ )
                {
                    if(st[i])
                    {
                        h = i + \'a\';
                        break;
                    }
                }                
                
                //找p
                int p;
                for(int i = k - 1; i >= 0; i --)
                {
                    if(s[i] != m)
                    {
                        p = i;  //找到p
                        break;
                    }
                }
                if(p == k - 1) p --;
                
                for (int i = 0; i < p; i ++ ) a[i] = s[i];
                for (int i = p + 2; i < k; i ++ ) a[i] = h;
                
                cos[0] = s[p], cos[1] = s[p + 1];
                f(cos);
                
                a[p] = ans[0];
                a[p + 1] = ans[1];
            }
        }
        //cout << ans[0] << \' \' << ans[1] << endl;
        for (int i = 0; i < k; i ++ ) cout << a[i];
        cout << endl;
    }
    
    return 0;
}

以上是关于AcWing第12场周赛-2021/8/14的主要内容,如果未能解决你的问题,请参考以下文章

Acwing第 34 场周赛

Acwing第 38 场周赛

AcWing第23场周赛题解

Acwing第 53 场周赛完结

Acwing第 31 场周赛完结

Acwing第 36 场周赛完结