Ultraviolet A 1462-fuzzy Google suggest (Dictionary tree + DFS)

Source: Internet
Author: User

Link to the question: Ultraviolet A 1462-fuzzy Google suggest

Simulate a fuzzy search by Google. given to a string set, there are then n searches. Each time there is an integer x and a string, which indicates that the string can be modified X times, this includes adding, modifying, and deleting a character. The modified character may be the prefix of the number of strings in the character set.

Solution: first, create a dictionary tree. For each search, perform DFS on the dictionary tree and process the data according to the position where the parameter x matches the string. Then, mark the position matching to the end as 2, then, for the second DFS, search for the first occurrence location of 2 on each branch.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 3000005;const int sigma_size = 26;struct Tire {    int sz;    int g[maxn][sigma_size];    int val[maxn], vis[maxn];    int deep, ans;    char s[15];    void init();    int idx(char ch);    void insert(char* str);    int solve(char* str, int x);    void dfs(int u, int d, int x);    void clear(int u, int flag);}AC;int main () {    int n, x;    char str[15];    while (scanf("%d", &n) == 1) {        AC.init();        for (int i = 0; i < n; i++) {            scanf("%s", str);            AC.insert(str);        }        scanf("%d", &n);        for (int i = 0; i < n; i++) {            scanf("%s%d", str, &x);            printf("%d\n", AC.solve(str, x));        }    }    return 0;}void Tire::init() {    sz = 1;    val[0] = 0;    //memset(vis, 0, sizeof(vis));    memset(g[0], 0, sizeof(g[0]));}int Tire::idx (char ch) {    return ch - ‘a‘;}void Tire::dfs (int u, int d, int x) {    if (x < 0)        return;    if (vis[u] == 0)        vis[u] = 1;    if (d == deep) {        vis[u] = 2;        return;    }    int v = idx(s[d]);    if (g[u][v])        dfs(g[u][v], d + 1, x);    dfs(u, d + 1, x - 1);    for (int i = 0; i < sigma_size; i++) {        if (g[u][i]) {            dfs(g[u][i], d, x - 1);            dfs(g[u][i], d + 1, x - 1);        }    }}void Tire::clear(int u, int flag) {    if (vis[u] == 0)        return;    if (flag && vis[u] == 2) {        ans += val[u];        flag = 0;    }    for (int i = 0; i < sigma_size; i++) {        if (g[u][i])            clear(g[u][i], flag);    }    vis[u] = 0;}int Tire::solve(char* str, int x) {    ans = 0;    deep = strlen(str);    strcpy(s, str);    dfs(0, 0, x);    clear(0, 1);    return ans;}void Tire::insert(char* str) {    int u = 0, n = strlen(str);    for (int i = 0; i < n; i++) {        int v = idx(str[i]);        if (g[u][v] == 0) {            val[sz] = 0;            memset(g[sz], 0, sizeof(g[sz]));            g[u][v] = sz++;        }        u = g[u][v];        val[u]++;    }}

Ultraviolet A 1462-fuzzy Google suggest (Dictionary tree + DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.