69-67: 模拟赛

模拟,注意知25推1的情况

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>

const int MaxN = 1005;

char m[128];
bool used_a[128], used_b[128];
char a[MaxN], b[MaxN], c[MaxN], d[MaxN];

char find_unused(bool *u) {
    for (char c = 'a'; c <= 'z'; ++c) {
        if (!u[c]) return c;
    }
    return -1;
}
int count_unused(bool *u) {
    int cnt = 0;
    for (char c = 'a'; c <= 'z'; ++c) {
        cnt += (int) u[c];
    }
    return cnt;
}

void err(void) {
    printf("ERROR\n");
    exit(0);
}

int main(void) 
{
    freopen("enc.in","r",stdin)    ;
    freopen("enc.out","w",stdout)    ;
    scanf("%s\n%s\n%s\n", a, b, c);

    int n = strlen(a);
    for (int i = 0; i < n; ++i) {
        if (m[b[i]] != 0 && m[b[i]] != a[i]) err();
        m[b[i]] = a[i];
        used_a[a[i]] = true;
        used_b[b[i]] = true;
    }

    if (count_unused(used_a) != count_unused(used_b))
        err();

    if (count_unused(used_a) == 25) {
        char a = find_unused(used_a);
        char b = find_unused(used_b);
        m[b] = a;
    }

    n = strlen(c);
    for (int i = 0; i < n; ++i) {
        if (m[c[i]] == 0) err();
        d[i] = m[c[i]];
    }

    printf("%s\n", d);
    fclose(stdin);
    fclose(stdout);

    return 0;
}

求 lcs

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <functional>
#include <utility>

using std::max;
using std::min;

const int MaxN = 100005;

int n;
int a[MaxN];
int inv[MaxN];

int main(void)
 {
     freopen("sort.in","r",stdin);
     freopen("sort.out","w",stdout);
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d", a+i);
    memset(inv, 0x7f, sizeof(inv));
    inv[0] = -1;
    
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        int v = std::lower_bound(inv, inv+ans+1, a[i]) - inv;
        ans = max(ans, v);
        inv[v] = min(inv[v], a[i]);
    }

    printf("%d\n", ans);
    fclose(stdin);
    fclose(stdout);
    return 0;
}

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>

const int MaxN = 5005;

int n;
int mat[MaxN][MaxN];
int cycle[MaxN], cyclelen;
int stack[MaxN], stop, spos[MaxN], vis[MaxN];

bool dfs(int x) {
    vis[x] = 1, stack[stop++] = x, spos[x] = stop-1;
    for (int i = 0; i < n; ++i) {
        if (mat[x][i]) {
            if (vis[i] == 1) {
                cyclelen = stop - spos[i];
                memcpy(cycle, stack + spos[i], sizeof(int) * cyclelen);
                return true;
            } else if (vis[i] == 0) {
                bool v = dfs(i);
                if (v) return true;
            }
        }
    }
    --stop, vis[x] = 2;
    return false;
}

void gen(void) {
    int m = cyclelen;
    for (int i = 1; i < m-1; ++i) {
        // check 1 i i+1
        if (mat[cycle[i+1]][cycle[0]]) {
            printf("%d %d %d\n", cycle[0]+1, cycle[i]+1, cycle[i+1]+1);
        }
    }
}
char tmp[MaxN];

int main(void) 
{
    freopen("game.in","r",stdin)    ;
    freopen("game.out","w",stdout)    ;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("\n%s", tmp);
        for (int j = 0; j < n; ++j) {
            mat[i][j] = tmp[j] == '1';
        }
    }
    for (int i = 0; i < n; ++i) {
        if (vis[i] == 0) {
            bool found = dfs(i);
            if (found) {
                gen();
                return 0;
            }
        }
    }
    printf("-1\n");
    fclose(stdin);
    fclose(stdout);
    return 0;

}

 

posted @ 2018-11-01 15:31  xayata  阅读(162)  评论(0编辑  收藏  举报