[字典树] poj 2001 Shortest Prefixes

AI驱动全栈开发实战:基于Spec与Codex的规格驱动开发指南 1. 先搞清楚“AI重构前端全栈”到底在解决什么实际问题 最近很多人在聊“AI重构前端全栈”,听起来像是又一个营销概念。但如果你真的拆开看,它核心解决的是一个非常具体且痛苦的问题: 一个开发者,如何从零开始,系统性地、不跑偏地完成一个完整项目,并且保证代码质量和架构一致性。 传统开发流程里,产品经理出PRD,前后端开会对齐,前端画页面,后端写接口,测试再介入。这个过程沟通成本高,文档容易过时,新人上手慢。而“AI重构”的思路,是试图用一套 规范化的指令(Spec) 来驱动AI(比如Codex这类模型)生成代 阅读详情

题目链接:

http://poj.org/problem?id=2001

Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 12631 Accepted: 5387

Description

A prefix of a string is a substring starting at the beginning of the given string. The prefixes of "carbon" are: "c", "ca", "car", "carb", "carbo", and "carbon". Note that the empty string is not considered a prefix in this problem, but every non-empty string is considered to be a prefix of itself. In everyday language, we tend to abbreviate words by prefixes. For example, "carbohydrate" is commonly abbreviated by "carb". In this problem, given a set of words, you will find for each word the shortest prefix that uniquely identifies the word it represents. 

In the sample input below, "carbohydrate" can be abbreviated to "carboh", but it cannot be abbreviated to "carbo" (or anything shorter) because there are other words in the list that begin with "carbo". 

An exact match will override a prefix match. For example, the prefix "car" matches the given word "car" exactly. Therefore, it is understood without ambiguity that "car" is an abbreviation for "car" , not for "carriage" or any of the other words in the list that begins with "car". 

Input

The input contains at least two, but no more than 1000 lines. Each line contains one word consisting of 1 to 20 lower case letters.

Output

The output contains the same number of lines as the input. Each line of the output contains the word from the corresponding line of the input, followed by one blank space, and the shortest prefix that uniquely (without ambiguity) identifies this word.

Sample Input

carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate

Sample Output

carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona

Source

[Submit]   [Go Back]   [Status]   [Discuss]


题目意思:

给n个串,求每个串的不和其他串冲突的前缀。

解题思路:

字典树。

保存一个包含当前路径的单词个数,当个数为1时,说明没有冲突可以作为前缀。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 1100
#define Maxm 26
char save[Maxn][25];

struct Node
{
    int cnt; //包含该路径的单词数
    struct Node * next[Maxm];
}root,node[Maxn*22];
int p;

void inse(char * word)
{
    Node * pos=&root;

    while(*word)
    {
        if(pos->next[*word-'a']==NULL) //之前没出现过
        {
            node[p].cnt=0; //初始化
            memset(node[p].next,NULL,sizeof(node[p]));//初始化
            pos->next[*word-'a']=&node[p++];//静态节点
        }
        pos=pos->next[*word-'a'];//往下
        pos->cnt++;
        word++;
    }
}

void query(char * word)
{
    Node * pos=&root;

    while(*word)
    {
        if(pos->cnt==1) //找到
            break;
        printf("%c",*word); //输出
        pos=pos->next[*word-'a'];
        word++;
    }
    putchar('\n');
}

int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   int k=0;
   p=0;
   memset(root.next,NULL,sizeof(root.next));

   while(~scanf("%s",save[k]))
        inse(save[k++]);

   for(int i=0;i<k;i++)
   {
       printf("%s ",save[i]);
       query(save[i]);

   }
   return 0;
}





从零到一:ArduPilot无人船PID调参实战指南 本文详细介绍了ArduPilot无人船PID调参的实战指南,从基础参数设定到高级调参技术,帮助用户掌握水面环境下的PID控制逻辑。通过案例分析和参数示例,解决无人船在空载、负载变化及不同水域环境中的航行问题,提升无人船的稳定性和精确度。 阅读详情

相关推荐

Agent本地部署教程详细步骤

在开始之前,你需要明确你要部署的Agent是什么。如果你需要部署其他Agent,请参考其官方文档调整步骤。

weixin_39673931的博客 2533

P - Shortest Prefixes

Time Limit: 1000MS   Memory Limit: 30000KB   64bit IO Format: %I64d & %I64u [Submit]   [Go Back]   [Status] Description

mr_lee 644

SAP SD RRB实战:从DIP参数文件到DP90/DP91开票的完整链路解析

本文详细解析了SAP SD模块中RRB(Resource-related Billing)业务场景的完整链路,从DIP参数文件配置到DP90/DP91开票操作。通过实战案例分享DIP参数文件的核心配置技巧、特性设置及物料决定要点,帮助用户掌握动态开票的关键技术,提升SAP SD RRB的实施效率。

weixin_26833939的博客 382

POJ2001 Shortest Prefixes(Trie树)

传送门 题目大意:对于每一个字符串,求一个它的前缀串,使这个前缀串能够唯一确定这个字符串。空串不能看做前缀。 还是更喜欢这种指针的写法。 code: #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <cmath> #include <string> #include <vector> #incl

一个小菜鸡的博客 274

洛奇 服务器文件,MabinogiG20S2 [洛奇G20服务端]

Code:Titles:>set_title /id: /add /remove Parameters for titles>set_title /id:60001 /add add devcat title>set_title /id:60001 /remove remove devcat title===========================...

weixin_30813385的博客 4327

Win10文件夹只读属性自动恢复?彻底解决方法解析

Win10文件夹只读属性自动恢复?彻底解决方法解析在Windows系统使用过程中,文件权限管理常会遇到棘手问题。最近多位用户反馈:取消文件夹只读属性后,系统却自动恢复该设置,导致无法正常复制或修改文件。本文将深入分析该现象的产生原因,并提供多种解决方案,帮助开发者彻底摆脱权限困扰。

mmoo_python的博客 7588

POJ 2001-Shortest Prefixes(字典树)

题意:就是给你一些字符串

KJBU2 640

ISO 15118-3协议栈开发避坑指南:从EvseSlac状态机设计看电动汽车充电握手难题

本文深入剖析了ISO 15118-3协议栈开发中的核心挑战,特别是SLAC握手流程的实现难点。以Everest项目的EvseSlac状态机为例,详细解读了从状态机设计、多厂商PLC芯片兼容性处理,到严苛的时序与超时控制等关键工程实践,为开发者提供了一套超越协议文档的、可直接落地的避坑指南和软件实现方案。

jump7的博客 276

Shortest Prefixes 字典树 (前缀树) 入门题 题解

Shortest Prefixes 题解(1)题目(2)题解 (1)题目 Shortest Prefixes A prefix of a string is a substring starting at the beginning of the given string. The prefixes of “carbon” are: “c”...

林深不见鹿 的博客 1586

【机械臂算法】机械臂动力学参数辨识仿真

本文以puma560m机械臂为例子进行动力学参数辨识的讲解,puma560m可以在robotic toolbox中找到,这里以它真实机械臂对他的动力学参数进行辨识。此外这里还有要说的是,机械臂参数辨识其实是一个系统工程,其中和系统的机械、电子、嵌入式都有着很深的联系,并不是仿真这么简单的。

vrijheid的博客 7862

深度学习论文: Cultivated Land Extraction from High-Resolution Remote Sensing Image

深度学习论文: Cultivated Land Extraction from High-Resolution Remote Sensing Image The Winning Solution to the iFLYTEK Challenge 2021 Cultivated Land Extraction from High-Resolution Remote Sensing Image PDF: https://arxiv.org/pdf/2202.10974.pdf PyTorch代码: https:

mingo_敏 3484

Shortest Prefixes(最短前缀)

A prefix of a string is a substring starting at the beginning of the given string. The prefixes of “carbon” are: “c”, “ca”, “car”, “carb”, “carbo”, and “carbon”. Note that the empty string is not cons...

weixin_44020969的博客 1325

rk3568 修改开机动画

bootanimation.zip是一个Android设备启动时播放的动画文件,它包含了一系列的图片和描述文件,用于描述动画的播放顺序和持续时间。

weixin_35723192的博客 2736

POJ - 2001 Shortest Prefixes从(字典树)

POJ - 2001 Shortest Prefixes从(字典树) A prefix of a string is a substring starting at the beginning of the given string. The prefixes of “carbon” are: “c”, “ca”, “car”, “carb”, “carbo”, and “carbon”. Not...

weixin_43179892的博客 387

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

POJ2001 Shortest Prefixes【字典树】

Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22096   Accepted: 9444 Description A prefix of a string is a substring starting at the beginning of the...

逐梦者 398

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

POJ 2001 Shortest Prefixes(字典树)

题目地址:POJ 2001 考察的字典树,利用的是建树时将每一个点只要走过就累加。最后从根节点开始遍历,当遍历到只有1次走过的时候,就说明这个地方是最短的独立前缀。然后记录下长度,输出即可。 代码如下: #include #include #include #include #include #include #include #include #include using

scf0920 1129

[poj 2001] Shortest Prefixes (字典树)

[poj 2001] Shortest Prefixes (字典树) Language: Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20503 Accepted: 8863 Description ...

Tricksterism 88

POJ-2001-Shortest Prefixes(字典树入门题)

Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20382 Accepted: 8819 Description A prefix of a string is a substring starting at the beginning of the given...

chenyang890的博客 516

【POJ】2001 - Shortest Prefixes(字典树)

点击打开题目 Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17357   Accepted: 7551 Description A prefix of a string is a substring star

wyg1997 582

【poj2001】Shortest Prefixes 字典树

DescriptionA prefix of a string is a substring starting at the beginning of the given string. The prefixes of “carbon” are: “c”, “ca”, “car”, “carb”, “carbo”, and “carbon”. Note that the empty string i

DQS的树洞 776

POJ - 2001Shortest Prefixes字典树记录

POJ - 2001 Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000KB   64bit IO Format: %I64d & %I64u Submit Status Description A prefix of a string is a substrin

qq_18661257的专栏 1233

POJ 2001 Shortest Prefixes【字典树】

Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15362   Accepted: 6621 Description A prefix of a string is a substring starting at the be

_阿阿阿阿欢 522

kyeremal-poj2001-Shortest Prefixes-字典树trie

poj2001-Shortest Prefixes Language: Default Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14756   Accepted: 6370 Description

kyeremal的专栏 732

POJ2001 Shortest Prefixes 字典树

  Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21761   Accepted: 9320 Description A prefix of a string is a substring starting at the beginning of ...

u011535421的博客 215

poj 2001 Shortest Prefixes(Tire树/字典树)

poj 2001 Shortest Prefixes(Tire树/字典树)

PKU_ZZY的博客 771

【POJ 2001 --- Shortest Prefixes】字典树

【POJ 2001 --- Shortest Prefixes】字典树 Description A prefix of a string is a substring starting at the beginning of the given string. The prefixes of “carbon” are: “c”, “ca”, “car”, “carb”, “carbo”, and ...

来日浅谈的博客 498

POJ 2001:Shortest Prefixes(字典树入门)

字典树入门题

温姑娘的博客 524
上一篇: [最小表示] zoj 1729 Hidden Password
下一篇: [字典树] poj 2418 Hardwood Species
Accagain
博客等级 码龄14年 376粉丝 316原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值