Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

和楼上的思路几乎一样,只是他的代码不怎么看的懂

Posted by Yick_Liao at 2017-05-20 17:46:01 on Problem 1816
可能我是弱鸡,想不明白你们一来就搞字典树和DFS的人,O(100000 * 100 * 常数)的时间复杂度,不应该直接爆么?
贴一个超时的代码,再贴一个AC代码,完全是考的编程能力。


//超时的代码
#include <stdio.h>
#include <string.h>
#define MAXN 100000
char ps[MAXN][7];
int answers[MAXN + 1];

int match(char *p, char *s) {
    int a = 0, b = 0, c, lenp = strlen(p), lens = strlen(s), ret = 0;
    char tp[7] = {0}, ts[21] = {0};
    while (a < lenp && b < lens) {
        if (p[a] == '*') {
            for (c = a + 1; c < lenp; ++c) {
                tp[c - a - 1] = p[c];
            }
            for (c = b; c < lens; ++c) {
                ts[c - b] = s[c];
            }
            for (c = b; c <= lens; ++c) {
                ret |= match(tp, ts + c - b);
            }
            return ret;
        } else if (p[a] == s[b] || p[a] == '?') {
            a += 1, b += 1;
        } else {
            return 0;
        }
    }
    while (a < lenp && p[a] == '*') {
        a += 1;
    }
    return ret | (a == lenp && b == lens);
}

int main() {
    int N, M, a, b;
    char word[21];
    scanf("%d%d", &N, &M);
    for (a = 0; a < N; ++a) {
        scanf("%s", ps + a);
    }
    for (a = 0; a < M; ++a) {
        scanf("%s", word);
        answers[0] = 0;
        for (b = 0; b < N; ++b) {
            if (match(ps[b], word)) {
                answers[++answers[0]] = b;
            }
        }
        if (!answers[0]) {
            printf("Not match\n");
            continue;
        }
        for (b = 1; b <= answers[0]; ++b) {
            printf("%d", answers[b]);
            putchar(b == answers[0] ? '\n' : ' ');
        }
    }
    return 0;
}

//AC的代码
#include <stdio.h>
#include <string.h>
#define MAXN 100000
char ps[MAXN][8], word[22];
int answers[MAXN + 1];

int match(int idx, int idxp, int idxs) {
    int a = idxp, b = idxs, ret = 0;
    while (a <= ps[idx][0] && b <= word[0]) {
        if (ps[idx][a] == '*') {
            while (b <= word[0]) {
                ret |= match(idx, a + 1, b);
                b += 1;
            }
            ret |= match(idx, a + 1, b);
            return ret;
        } else if (ps[idx][a] == word[b] || ps[idx][a] == '?') {
            a += 1, b += 1;
        } else {
            return 0;
        }
    }
    while (a <= ps[idx][0] && ps[idx][a] == '*') {
        a += 1;
    }
    return ret | (a == ps[idx][0] + 1 && b == word[0] + 1);
}

int main() {
    int N, M, a, b, len;
    scanf("%d%d", &N, &M);
    for (a = 0; a < N; ++a) {
        scanf("%s", ps[a]);
        len = strlen(ps[a]);
        for (b = len; b > 0; --b) {
            ps[a][b] = ps[a][b - 1];
        }
        ps[a][0] = len;
    }
    for (a = 0; a < M; ++a) {
        scanf("%s", word + 1);
        word[0] = strlen(word + 1);
        answers[0] = 0;
        for (b = 0; b < N; ++b) {
            if (match(b, 1, 1)) {
                answers[++answers[0]] = b;
            }
        }
        if (!answers[0]) {
            printf("Not match\n");
            continue;
        }
        for (b = 1; b <= answers[0]; ++b) {
            printf("%d", answers[b]);
            putchar(b == answers[0] ? '\n' : ' ');
        }
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator