Codeforces Round #498 (Div. 3) - 赛后补题

D. Two Strings Swaps

PS:没思考清楚,重复算了一些情况。

#include<bits/stdc++.h>
#include<bitset>
#define ll long long
#define P pair<int, int>
#define PP pair<int,pair<int, int>>
#define pb push_back
#define pp pop_back
#define lson root << 1
#define INF (int)2e9 + 7
#define rson root << 1 | 1
#define LINF (unsigned long long int)1e18
#define mem(arry, in) memset(arry, in, sizeof(arry))
using namespace std;

const int maxn = 1e5 + 5;

int n;
char a[maxn], b[maxn];

int main()
{
    cin >> n;
    scanf("%s", a + 1);
    scanf("%s", b + 1);
    int cnt = 0;
    for(int i = 1; i <= n / 2; i++) {
        int x = i;
        int y = n - i + 1;
        if((a[x] == a[y]) && (b[x] == b[y])) continue;
        if((a[x] == b[x]) && (a[y] == b[y])) continue;
        if((a[x] == b[y]) && (b[x] == a[y])) continue;
    
        if(a[x] == b[x] || a[x] == b[y] || b[x] == b[y] || a[y] == b[y] || a[y] == b[x]) {
            cnt++;
        }
        else cnt += 2;
    }

    if(n % 2 != 0) {
        if(a[n / 2 + 1] != b[n / 2 + 1]) cnt++;
    }
    cout << cnt << endl;
    
    return 0;
}

 E. Military Problem

PS:被D坑了,这题超级水,在节点处都不用排序

#pragma warning(disable:4996)
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 2e5 + 5;

int n, q, cnt;
int ord[maxn], in[maxn], out[maxn];
vector<int> mp[maxn];

void DFS(int u) {
    ord[++cnt] = u;
    in[u] = cnt;
    //sort(mp[u].begin(), mp[u].end());
    for (auto v : mp[u]) DFS(v);
    out[u] = cnt;
}

int main()
{
    while (scanf("%d %d", &n, &q) != EOF) {
        cnt = 0;
        for (int i = 2; i <= n; i++) {
            int tp;
            scanf("%d", &tp);
            mp[tp].push_back(i);
        }
        DFS(1);
        while (q--) {
            int u, k;
            scanf("%d %d", &u, &k);
            int pos = in[u] + k - 1;
            if (pos > out[u]) puts("-1");
            else {
                printf("%d\n", ord[pos]);
            }
        }
    }
    return 0;
}

 F. Xor-Paths

PS:没想过这种做法,首尾向中间搜。复杂度O(2 * 2 ^( n + m ) / 2)

#include<bits/stdc++.h>
#include<bitset>
#define ll long long
#define P pair<int, int>
#define PP pair<int,pair<int, int>>
#define pb push_back
#define pp pop_back
#define lson root << 1
#define INF (int)2e9 + 7
#define rson root << 1 | 1
#define LINF (unsigned long long int)1e18
#define mem(arry, in) memset(arry, in, sizeof(arry))
using namespace std;

const int maxn = 2e5 + 5;

int n, m;
ll k, mp[30][30];

map<ll, ll> dp[25];

void DFS(int x, int y, ll s) {
    if(x > n || y > m) return;
    if((x + y) == (n + m) / 2 + 1) {
        dp[x][s ^ mp[x][y]]++;
        return;
    }
    DFS(x + 1, y, s ^ mp[x][y]);
    DFS(x, y + 1, s ^ mp[x][y]);
}

ll Solve(int x, int y, ll s) {
    if(x < 1 || y < 1) return 0;
    if((x + y) == (n + m) / 2 + 1) return dp[x][s];
    return Solve(x - 1, y, s ^ mp[x][y]) + Solve(x, y - 1, s ^ mp[x][y]);
}

int main()
{
    cin >> n >> m >> k;
    for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) cin >> mp[i][j];
    DFS(1, 1, 0);
    printf("%I64d\n", Solve(n, m, k));
    return 0;
}

 

posted @ 2018-07-17 13:21  天之道,利而不害  阅读(151)  评论(0编辑  收藏  举报