Bless You Autocorrect! 字典树+bfs

本文介绍了一种利用手机自动纠正功能减少打字次数的方法,通过预先设置常用词汇,当输入部分字符时,手机会自动建议完整单词,按下Tab键即可完成输入,即使建议单词不完全匹配目标单词,也可通过删除多余字符来节省打字步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
#include<bits/stdc++.h> 
using namespace std;
#define rep(i,s,t) for(int i=s;i<t;++i)
#define repb(i,s,t) for(int i=s;i<=t;i++)
#define mst(a,b) memset(a,b,sizeof(a)) 
#define pb push_back
#define maxn 1100004
const int inf =0x3f3f3f3f; 

vector<int> N[maxn];
int dis[maxn*2],inq[maxn*2];

struct trie{
	int ch[maxn][26],val[maxn],cnt[maxn],fa[maxn],ed[maxn],sz;
	void init(){sz=0;mst(cnt,0);mst(ch,0);mst(val,0);mst(fa,0);mst(ed,0);}
	int idx(char c){return c-'a';}
	void ins(char str[]){
		int u=0,c,h=-1;
		for(int i=0;str[i];++i){
			c=idx(str[i]);
			if(!ch[u][c]){
				ch[u][c]=++sz;
				if(h==-1)h=ch[u][c];
			}
			N[u].pb(ch[u][c]);
			N[ch[u][c]].pb(u);
			u=ch[u][c];
		}
		if(h!=-1){
			N[h].pb(u);
		}
	}
	void build(){
		queue<int> q;
		mst(dis,0x3f);mst(inq,0);
		q.push(0);dis[0]=0;
		while(!q.empty()){
			int u=q.front();q.pop();inq[u]=0;
			for(auto v:N[u]){
				if(dis[v]>dis[u]+1){
					dis[v]=dis[u]+1;
					if(!inq[v]){
						q.push(v);
						inq[v]=1;
					}
				}
			}
		}
	}
	int work(char str[]){
		int len=strlen(str),u=0,c,ans=len;
		for(int i=0;str[i];i++){
			c=idx(str[i]);
			if(ch[u][c])u=ch[u][c];
			else break;
			ans=min(ans,dis[u]+len-i-1);
		}
		return ans;
	}
}T;

char str[maxn];

int main() {
	int n,m;
	T.init();
	scanf("%d%d",&n,&m);
	repb(i,1,n){
		scanf("%s",str);
		T.ins(str);
	}
	T.build();
	repb(i,1,m){
		scanf("%s",str);
		printf("%d\n",T.work(str));
	}
    return 0;
}

 

内容概要:该论文研究增程式电动汽车(REEV)的能量管理策略,针对现有优化策略实时性差的问题,提出基于工况识别的自适应等效燃油消耗最小策略(A-ECMS)。首先建立整车Simulink模型和基于规则的策略;然后研究动态规划(DP)算法和等效燃油最小策略;接着通过聚类分析将道路工况分为四类,并设计工况识别算法;最后开发基于工况识别的A-ECMS,通过高德地图预判工况类型并自适应调整SOC分配。仿真显示该策略比规则策略节油8%,比简单SOC规划策略节油2%,并通过硬件在环实验验证了实时可行性。 适合人群:具备一定编程基础,特别是对电动汽车能量管理策略有兴趣的研发人员和技术爱好者。 使用场景及目标:①理解增程式电动汽车能量管理策略的基本原理;②掌握动态规划算法和等效燃油消耗最小策略的应用;③学习工况识别算法的设计和实现;④了解基于工况识别的A-ECMS策略的具体实现及其优化效果。 其他说明:此资源不仅提供了详细的MATLAB/Simulink代码实现,还深入分析了各算法的原理和应用场景,适合用于学术研究和工业实践。在学习过程中,建议结合代码调试和实际数据进行实践,以便更好地理解策略的优化效果。此外,论文还探讨了未来的研究方向,如深度学习替代聚类、多目标优化以及V2X集成等,为后续研究提供了思路。
内容概要:论文《基于KANN-DBSCAN带宽优化的核密度估计载荷谱外推》针对传统核密度估计(KDE)载荷外推中使用全局固定带宽的局限性,提出了一种基于改进的K平均最近邻DBSCAN(KANN-DBSCAN)聚类算法优化带宽选择的核密度估计方法。该方法通过对载荷数据进行KANN-DBSCAN聚类分组,采用拇指法(ROT)计算各簇最优带宽,再进行核密度估计和蒙特卡洛模拟外推。实验以电动汽车实测载荷数据为对象,通过统计参数、拟合度和伪损伤三个指标验证了该方法的有效性,误差显著降低,拟合度R²>0.99,伪损伤接近1。 适合人群:具备一定编程基础和载荷数据分析经验的研究人员、工程师,尤其是从事汽车工程、机械工程等领域的工作1-5年研发人员。 使用场景及目标:①用于电动汽车载荷谱编制,提高载荷预测的准确性;②应用于机械零部件的载荷外推,特别是非对称载荷分布和多峰扭矩载荷;③实现智能网联汽车载荷预测与数字孪生集成,提供动态更新的载荷预测系统。 其他说明:该方法不仅解决了传统KDE方法在复杂工况下的“过平滑”与“欠拟合”问题,还通过自适应参数机制提高了方法的普适性和计算效率。实际应用中,建议结合MATLAB代码实现,确保数据质量,优化参数并通过伪损伤误差等指标进行验证。此外,该方法可扩展至风电装备、航空结构健康监测等多个领域,未来研究方向包括高维载荷扩展、实时外推和多物理场耦合等。
内容概要:该论文深入研究了直流微电网中的电能质量问题,特别是纹波污染对电网的影响。文章提出了使用直流有源滤波器(DC-APF)来抑制纹波,并创新性地将储能系统与DC-APF结合,实现交流侧无功补偿。研究内容涵盖纹波特性分析、DC-APF拓扑选择、基于数字滤波器的纹波提取策略、双幂次趋近律滑模控制设计,以及新型组合装置的无功补偿功能验证。仿真结果表明,所提方法在纹波抑制和无功补偿方面表现出色,能够有效提升直流微电网的电能质量。此外,论文还详细介绍了各个技术模块的具体实现代码,并通过多场景对比仿真验证了控制策略的有效性和优越性。 适合人群:具备电力电子、自动控制或相关专业背景的研究人员和技术工程师,尤其是对直流微电网电能质量治理感兴趣的从业者。 使用场景及目标:①理解和掌握直流微电网中纹波产生机理及其对系统性能的影响;②学习DC-APF的设计与实现,包括H桥拓扑、滑模控制器等核心技术;③探索储能系统与DC-APF组合装置在无功补偿方面的应用潜力;④评估不同控制策略在实际工程环境中的表现,为优化设计方案提供参考。 其他说明:本文不仅提供了理论分析和算法推导,还给出了完整的Python代码示例,便于读者动手实践。同时,文中涉及的技术细节和仿真结果对于推动直流微电网领域的技术创新具有重要价值。建议读者结合自身需求,重点关注感兴趣的部分,并尝试复现相关实验以加深理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值