题目:https://codeforces.com/contest/1997


A. Strong Password

<code>#include &lt;bits/stdc++.h&gt;
using namespace std;
// #pragma GCC optimize(3, "Ofast", "inline") // O3优化
#define endl "\n"
#define ll long long

char getChar(char before)
{
    char i = 'a';
    for (; i == before; i++)
        ;
    return i;
}

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    int t;
    cin &gt;&gt; t;

    while (t--) {
        string s;
        cin &gt;&gt; s;
        for (int i = 0; i &lt;= s.length() - 1; i++) {
            if (i == s.length() - 1) {
                s.append(1, getChar(s[i]));
                break;
            } else {
                if (s[i] == s[i + 1]) {
                    s.insert(i + 1, 1, getChar(s[i]));
                    break;
                }
            }
        }
        cout &lt;&lt; s &lt;&lt; endl;
    }

    return 0;
}</code>

B. Make Three Regions

<code>#include &lt;bits/stdc++.h&gt;
using namespace std;
// #pragma GCC optimize(3, "Ofast", "inline") // O3优化
#define endl "\n"
#define ll long long

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    int t;
    cin &gt;&gt; t;

    while (t--) {
        int n;
        cin &gt;&gt; n;
        string s[2];
        cin &gt;&gt; s[0] &gt;&gt; s[1];
        int cnt = 0;
        for (int i = 0; i &lt; 2; i++) {
            for (int j = 1; j &lt; n - 1; j++) {
                if (s[i][n == '.']) {
                    if (s[(i + 1) % 2][j - 1] == 'x' &amp;&amp; s[(i + 1) % 2][j + 1] == 'x' &amp;&amp; s[(i + 1) % 2][j] == '.' &amp;&amp; s[i][j - 1] == '.' &amp;&amp; s[i][j + 1] == '.') {
                        cnt++;
                    }
                }
            }
        }
        cout &lt;&lt; cnt &lt;&lt; endl;
    }

    return 0;
}</code>

C. Even Positions

<code>#include &lt;bits/stdc++.h&gt;
using namespace std;
// #pragma GCC optimize(3, "Ofast", "inline") // O3优化
#define endl "\n"
#define ll long long

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    int t;
    cin &gt;&gt; t;

    while (t--) {
        int n;
        string s;
        cin &gt;&gt; n &gt;&gt; s;
        int cnt_l = 0;
        ll step = 0;
        for (int i = 0; i &lt; n; i++) {
            if (s[i] == '_') {
                if (cnt_l &gt; 0) {
                    cnt_l--;
                } else {
                    cnt_l++;
                }
            } else {
                if (s[i] == '(') {
                    cnt_l++;
                } else {
                    cnt_l--;
                }
            }
            step += cnt_l;
        }
        cout &lt;&lt; step &lt;&lt; endl;
    }

    return 0;
}</code>

D. Maximize the Root

<code>#include &lt;bits/stdc++.h&gt;
using namespace std;
// #pragma GCC optimize(3, "Ofast", "inline") // O3优化
#define endl "\n"
#define ll long long

const int N = 2e5 + 5;

struct Node {
    int value;
    int parent;
    vector&lt;int&gt; sons;
};

Node tree[N];

ll postorder(int ind)
{
    if (tree[ind].sons.empty()) {
        return tree[ind].value;
    }

    ll sonsMin = INFINITY;
    for (auto&amp;&amp; i : tree[ind].sons) {
        sonsMin = min(postorder(i), sonsMin);
    }

    if (ind == 1) {
        return sonsMin;
    }

    if (sonsMin &lt;= tree[ind].value) {
        return sonsMin;
    } else {
        return (sonsMin + tree[ind].value) &gt;&gt; 1;
    }
}

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    int t;
    cin &gt;&gt; t;

    while (t--) {
        memset(tree, 0, sizeof(tree));
        int n;
        cin &gt;&gt; n;
        for (int i = 1; i &lt;= n; i++) {
            cin &gt;&gt; tree[i].value;
        }
        for (int i = 2; i &lt;= n; i++) {
            int parent;
            cin &gt;&gt; parent;
            tree[i].parent = parent;
            tree[parent].sons.push_back(i);
        }

        cout &lt;&lt; tree[1].value + postorder(1) &lt;&lt; endl;
    }

    return 0;
}</code>