[tire+最短路]Bless You Autocorrect!

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.

输入

The first line of input contains two positive integers n (1 ≤ n ≤ 105 ), the number of words in the dictionary, and m (1 ≤ m ≤ 10 5 ), 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.

输出

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

样例输入

5 5

austria

autocorrect

program

programming

computer

autocorrelation

programming

competition

zyx

austria

样例输出

12

4

11

3

2

建个tire树,然后连边,跑最短路。

看懂题不难

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+10;
const int inf=0x3f3f3f3f;
struct Tire{
int nex[maxn][26],l,val[maxn],dist[maxn];
bool vis[maxn];
struct Edge{
int v,cost;
Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
struct qnode{
int v,c;
qnode(int _v=0,int _c=0):v(_v),c(_c){}
bool operator<(const qnode &r)const{
return c>r.c;
}
};
vector<Edge>vt[maxn];
int newnode(){
for(int i=0;i<26;++i){
nex[l][i]=-1;
}
dist[l]=inf;
l++;
return l-1;
}
void init(){
l=0;
newnode();
memset(val,0,sizeof(val));
}
void add(int u,int v,int w){
vt[u].push_back(Edge(v,w));
}
void Insert(char *buf){
int len=strlen(buf);
int now=0,en;
for(int i=0;i<len;i++){
if(nex[now][buf[i]-'a']==-1){
nex[now][buf[i]-'a']=newnode();
}
add(now,nex[now][buf[i]-'a'],1);
add(nex[now][buf[i]-'a'],now,1);
now=nex[now][buf[i]-'a'];
val[now]++;
}
int temp;en=now;
now=0;
for(int i=0;i<len-1;i++){
temp=nex[now][buf[i]-'a'];
if(val[temp]==1)
add(temp,en,1);
now=temp;
}
}
void dijkstra(){
memset(vis,0,sizeof(vis));
priority_queue<qnode>que;
dist[0]=0;
que.push(qnode(0,0));
qnode tmp;
while(!que.empty()){
tmp=que.top();que.pop();
int u=tmp.v;
if(vis[u])continue;
vis[u]=true;
int len=vt[u].size();
for(int i=0;i<len;++i){
int v=vt[u][i].v;
int cost=vt[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost){
dist[v]=dist[u]+cost;
que.push(qnode(v,dist[v]));
}
}
}
}
int solve(char *buf){
int len=strlen(buf);
int now=0,i;
for(i=0;i<len;++i){
if(nex[now][buf[i]-'a']==-1){
break;
}
now=nex[now][buf[i]-'a'];
}
return dist[now]+len-i;
}
}st;
char s[maxn];
int Scan() { int res = 0, flag = 0;char ch;if ((ch = getchar()) == '-') { flag = 1; }
else if(ch >= '0' && ch <= '9')
{
res = ch - '0';
}
while ((ch = getchar()) >= '0' && ch <= '9')
{
res = res * 10 + (ch - '0');
}
return flag ? -res : res;
}
int main()
{
int n,m;n=Scan();m=Scan();
st.init();
for(int i=1;i<=n;++i){
scanf("%s",s);
st.Insert(s);
}
st.dijkstra();
for(int i=1;i<=m;i++){
scanf("%s",s);
printf("%d\n",st.solve(s));
}
return 0;
}

[tire+最短路]Bless You Autocorrect!的更多相关文章

  1. Bless You Autocorrect!

    题目链接: https://odzkskevi.qnssl.com/0c87453efec2747f8e8a573525fd42f9?v=1533651456 题解: 这是一道Trie+BFS的题目: ...

  2. Urozero Autumn 2016. NCPC 2016

    A. Artwork 倒过来并查集维护即可. #include<cstdio> #include<algorithm> using namespace std; const i ...

  3. Nordic Collegiate Programming Contest (NCPC) 2016

    A Artwork B Bless You Autocorrect! C Card Hand Sorting D Daydreaming Stockbroker 贪心,低买高卖,不要爆int. #in ...

  4. bzoj1001--最大流转最短路

    http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...

  5. 【USACO 3.2】Sweet Butter(最短路)

    题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...

  6. Sicily 1031: Campus (最短路)

    这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...

  7. 最短路(Floyd)

    关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...

  8. bzoj1266最短路+最小割

    本来写了spfa wa了 看到网上有人写Floyd过了 表示不开心 ̄へ ̄ 改成Floyd试试... 还是wa ヾ(。`Д´。)原来是建图错了(样例怎么过的) 结果T了 于是把Floyd改回spfa 还 ...

  9. HDU2433 BFS最短路

    Travel Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

随机推荐

  1. Session简单介绍

    Session 会话 , Session是基于Cookie的一种会话机制. Cookie是服务器返回一小份数据给客户端,并且存放在客户端上. Session是,数据存放在服务器端. 常用API //得 ...

  2. DES与MD5加密

    using System; using System.Data; using System.Configuration; using System.Web; using System.Security ...

  3. 一天一个设计模式——(Singleton)单例模式(线程安全性)

    一.模式说明 有时候,我们希望在应用程序中,仅生成某个类的一个实例,这时候需要用到单例模式. 二.模式类图 三.模式中的角色 Singleton角色,该模式中仅有的一个角色,该角色有一个返回唯一实例的 ...

  4. JSP三大指令(Page指令,include指令,taglib指令)

    参考文章: https://www.runoob.com/jsp/jsp-directives.html http://c.biancheng.net/view/1458.html https://b ...

  5. hdu2457(最少替换多少个字符使主串不包含模式串)ac自动机+dp

    题:http://acm.hdu.edu.cn/showproblem.php?pid=2457 题意:给定n个模式串,给定一个主串,问最替换掉多少个字符使主串不包含模式串或输出“-1”表示没有可行的 ...

  6. (函数)P1149 火柴棒等式

    题解: #include<stdio.h>int a[10]={6,2,5,5,4,5,6,3,7,6};int num(int n){                          ...

  7. SeetaFaceEngine系列1:Face Detection编译和使用

    SeetaFace,根据GitHub上的介绍,就是一个开源的人脸检测.矫正和识别的开源库,是采用C++来编写的,并且是在CPU上执行的,没有用到GPU,但是可以用SSE或者OpenMP来加速.整个库分 ...

  8. modbus 指令16 $10 的格式

    { //写多个请求 01(从设备)10(功能码) 00 77(起始地址) 00 01(寄存器数) 02(字节数) 05 55(写的数据) 6F B8(CRC) //写多个返回 01(从设备) 10(功 ...

  9. 下面介绍mysql中模糊查询的四种用法:

    下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...

  10. python3 str.encode bytes.decode

    str.encode 把字符串编码成字节序列 bytes.decode 把字节序列解码成字符串 https://docs.python.org/3.5/library/stdtypes.html st ...