csuoj2023-bless you autocorrect(字符串,trie字典树,bfs最短路)

Description

Typing on phones can be tedious. It is easy to make typing mistakes, which is why most phones come with an autocorrect feature. Autocorrect not only fixes common typos, but also suggests how to finish the word while you type it. Jenny has recently been pondering how she can use this feature to her advantage, so that she can send a particular message with the minimum amount of typing.

The autocorrect feature on Jenny’s phone works like this: the phone has an internal dictionary of words sorted by their frequency in the English language. Whenever a word is being typed, autocorrect suggests the most common word (if any) starting with all the letters typed so far. By pressing tab, the word being typed is completed with the autocorrect suggestion. Autocorrect can only be used after the first character of a word has been typed – it is not possible to press tab before having typed anything. If no dictionary word starts with the letters typed so far, pressing tab has no effect.

Jenny has recently noticed that it is sometimes possible to use autocorrect to her advantage even when it is not suggesting the correct word, by deleting the end of the autocorrected word. For instance, to type the word “autocorrelation”, Jenny starts typing “aut”, which then autocorrects to “autocorrect” (because it is such a common word these days!) when pressing tab. By deleting the last two characters (“ct”) and then typing the six letters “lation”, the whole word can be typed using only 3 (“aut”) + 1 (tab) + 2 (backspace twice) + 6 (“lation”) = 12 keystrokes, 3 fewer than typing “autocorrelation” without using autocorrect.

Given the dictionary on the phone and the words Jenny wants to type, output the minimum number of keystrokes required to type each word. The only keys Jenny can use are the letter keys, tab and backspace.

Input

There will be several test cases. Each case begins with two positive integers n (1 ≤ n ≤ 105), the number of words in the dictionary, and m (1 ≤ m ≤ 105), the number of words to type. Then follow n lines with one word per line, sorted in decreasing order of how common the word is (the first word is the most common). No word appears twice in the dictionary. Then follow m lines, containing the words to type.

The dictionary and the words to type only use lower case letters ‘a’-‘z’. The total size of the input file is at most 1 MB.

Output

For each word to type, output a line containing the minimum number of keystrokes required to type the corresponding word.

Sample Input

5 5
austria
autocorrect
program
programming
computer
autocorrelation
programming
competition
zyx
austria

5 3
yogurt
you
blessing
auto
correct
bless
you
autocorrect

Sample Output

12
4
11
3
2

5
3
9

Hint

Source

NCPC 2016

题目大意:按手机中最常用的顺序给出n个单词的字典,输入过程中,当已输入的字符为字典中某单词的前缀时可按tab键自动完成该单词的输入(每次只弹出一个最常用的单词),m次字符串询问,每次询问要输入完该字符串最少需要多少次操作。输入,删除一个字符和按TAB键选择字典中的单词算为一次操作。

思路分析:可先对这n个单词建字典树,同时bfs扫一遍,记录各棵前缀树到0树的最短路,查询的字符串的最小操作数即为每个前缀的最小操作数与剩下手动输入数和的最小值。

!!!注意数组尽量开大一点,由于题目没给字符串的最大长度,连续RE了4次~~
代码
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<set>
#include<queue>
using namespace std;
set<int>A[1500010];
set<int>::iterator it;
queue<int>q;
char s[1100000];
int T[1800010][27],cnt[1800010],k,dp[1800010];
void build(char*s){
     int v,n=strlen(s),i,j,f=0;
     for(i=0;i<n;i++){
        v=s[i]-'a';
        if(T[f][v]==0){
            k++;
            T[f][v]=k;
        }
        cnt[T[f][v]]++;
        A[f].insert(T[f][v]);
        A[T[f][v]].insert(f);
        f=T[f][v];
     }
     int m=f;
     f=0;
     for(i=0;i<n-1;i++){
        v=s[i]-'a';
        if(cnt[T[f][v]]==1){
                A[T[f][v]].insert(m);
        }
        f=T[f][v];
     }
}
void bfs(){
    int u,v,i;
    for(i=0;i<=1500000;i++) dp[i]=100000000;
    dp[0]=0;
    for(q.push(0);!q.empty();q.pop()){
        u=q.front();
        for(it=A[u].begin();it!=A[u].end();it++){
            v=*it;
            if(dp[v]>dp[u]+1){
                dp[v]=dp[u]+1;
                q.push(v);
            }
        }
    }
}
int ques(char*s){
    int f=0,i,j,n=strlen(s),min;
    min=n;
    for(i=0;i<n;i++){
        int v=s[i]-'a';
        if(T[f][v]==0) break;
        if(min>dp[T[f][v]]+n-i-1) min=dp[T[f][v]]+n-i-1;
        f=T[f][v];
    }
    return  min;
}
int main(){
    int n,m,i,j;
    while(scanf("%d%d",&n,&m)!=EOF){
        k=0;
        memset(T,0,sizeof(T));
        memset(cnt,0,sizeof(cnt));
        for(i=0;i<=1000000;i++) A[i].clear();
        for(i=1;i<=n;i++){
            scanf("%s",&s);
            build(s);
        }
        bfs();
        for(i=1;i<=m;i++){
            scanf("%s",&s);
            int ans=ques(s);
            printf("%d\n",ans);
        }
        printf("\n");
    }
    return 0;
}

/**********************************************************************
	Problem: 2023
	User: guogai13
	Language: C++
	Result: AC
	Time:568 ms
	Memory:375656 kb
**********************************************************************/

猜你喜欢

转载自blog.csdn.net/guogai13/article/details/79871614
You
今日推荐