Codeforces Round #750 (Div. 2)

C. Grandma Capa Knits a Scarf

思路:

官方的思路真的牛皮,字符串双指针确实平时打得少。由于题目中说我们只选择一种字符,所以我们可以枚举所有的26中小写字母,然后用双指针,一个指开头,一个指结尾,两个字符相等就双指针都移动,如果不相等,看当前指向的字符是不是可以删掉,即等于当前所遍历的字符ch,如果相等则说明可以删掉当前字母,删掉字母的数量cnt++,否则说明当前遍历的字母不能构成回文,因为前后两个对称的字母又不相等,又没法删掉一个字母,所以接着遍历下一个字母,每次取个最小值就可以了

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <map>
#include <unordered_set>
#include <unordered_map>

#define x first
#define y second
#define IOS ios::sync_with_stdio(false);cin.tie(0);

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 65, M = 200010, MOD = 1000000007, INF = 0x3f3f3f3f;
const double PI = acos(-1);

int a[N];

int main()
{
    IOS;
    int T;
    cin >> T;
    while(T -- )
    {
        int n;
        string s;
        cin >> n >> s;
        int res = 0x3f3f3f3f;
        for (int ch = 0; ch < 26; ch ++ )
        {
            int l = 0, r = n - 1, cnt = 0;
            while(l <= r)
            {
                if(s[l] == s[r])
                    l++, r--;
                else if(s[l] == char(ch + 'a'))
                    l++, cnt++;
                else if(s[r] == char(ch + 'a'))
                    r--, cnt++;
                else 
                {
                    cnt = 0x3f3f3f3f;
                    break;
                }

            }
            res = min(res, cnt);
        }

        if(res == 0x3f3f3f3f)
            res = -1;
        cout << res << endl;
    }

    return 0;
}

D. Vupsen, Pupsen and 0

思路:

简单构造,首先考虑偶数的时候,很容易想到两项一组,一正一负。那奇数的时候就前三项一组就可以了,就具体分三种情况就可以。

然后说下很坑的点:

要开long long

printf不知道为啥不行,必须cout

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <map>
#include <unordered_set>
#include <unordered_map>

#define x first
#define y second
#define IOS ios::sync_with_stdio(false);cin.tie(0);

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 200010;
const double PI = acos(-1);

LL a[N];

int main()
{
    IOS;
    int T;
    cin >> T;
    while(T -- )
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n; i ++ )
            cin >> a[i];

        if(n & 1)
        {
            if(a[1] + a[2] != 0)
                cout << a[3] << ' ' << a[3] << ' ' << -(a[1] + a[2]) << ' ';
            else if(a[1] + a[3] != 0)
                cout << a[2] << ' ' << -(a[1] + a[3]) << ' ' << a[2] << ' ';
            else
                cout << -(a[3] + a[2]) << ' ' << a[1] << ' ' << a[1] << ' ';

            for (int i = 4; i <= n; i += 2)
                cout << -a[i + 1] << ' ' << a[i] << ' ';
            cout << endl;
        }
            
        else
        {
            for (int i = 1; i <= n; i += 2)
                cout << -a[i + 1] << ' ' << a[i] << ' ';
             cout << endl;
        }
    }

    return 0;
}

 

posted @ 2021-10-26 20:44  彦辰kkkkk  阅读(97)  评论(0)    收藏  举报