HDU 7064 - Singing Superstar


https://acm.hdu.edu.cn/showproblem.php?pid=7064

  • 注意开数组的大小
  • 注意优化(插入的时候判断长度是否可行)
const int maxn = 1e5 + 7;

int n, t, m, stp = 30;
int nex[maxn * 33][27], cnt;
vector<int> vec[maxn * 40];

void insert(char *s) {
    int l = strlen(s);
    for (int i = 0; i < l; i++) {
        int p = 0;
        for (int j = i; j < i + stp && j < l; j++) {
            int c = s[j] - 'a';
            if (!nex[p][c])
                nex[p][c] = ++cnt;
            p = nex[p][c];
            if (vec[p].size() == 0 || j - vec[p].back() >= j - i + 1)
                vec[p].push_back(j);
        }
    }
}

int find(char *s) {
    int l = strlen(s), p = 0;
    for (int i = 0; i < l; i++) {
        int c = s[i] - 'a';
        if (!nex[p][c])
            nex[p][c] = ++cnt;
        p = nex[p][c];
    }
    return vec[p].size();
}

char s[maxn];
char pt[maxn];

void solve() {
    scanf("%d", &t);
    while (t--) {
        scanf("%s", s);
        insert(s);
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%s", pt);
            int x = find(pt);
            printf("%d\n", x);
        }
        for (int i = 0; i <= cnt; i++) {
            vec[i].clear();
            for (int j = 0; j <= 26; j++)
                nex[i][j] = 0;
        }
        cnt = 0;
    }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM