<题目链接>

题目大意:

输入7e4个长度为9的字符串,每个字符串中只出现0~9这几种数字,现在需要你输出每个母串中最短的特有子串。

解题分析:

利用Trie树进行公共子串的判定,因为Trie树的特性是对节点的前缀字符串进行操作,所以为了转换成对母串中任意区间的字符串进行操作,我们对母串中的所有后缀字符串建树。下面用了一个比较优秀的Trie树模板。

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int N = 7e4+;
const int INF = 1e6;
char buf[N][];
int n,cur;
struct Trie{
int nxt[N*][],num[N*],time[N*];
int root,pos;
int newnode(){
for(int i=;i<;i++)nxt[pos][i]=-;
num[pos]=,time[pos]=;
return pos++;
}
void init(){
pos=;root=newnode();
}
void insert(char str[]){
int len=strlen(str);
int now=root;
for(int i=;i<len;i++){
int to=str[i]-'';
if(nxt[now][to]==-)
nxt[now][to]=newnode();
now=nxt[now][to];
if(time[now]!=cur) //建立时间戳的意义是,避免相同母串中不同位置中的相同子串产生重复标记
num[now]++,time[now]=cur; //num[now]记录下含有这个子串的母串数量
}
}
int query(char str[]){
int len=strlen(str);
int now=root;
for(int i=;i<len;i++){
now=nxt[now][str[i]-''];
if(num[now]==)return i;
}
return -;
}
}trie; int main(){
ios::sync_with_stdio(false);cin.tie();cout.tie();
cin>>n;
trie.init();
for(int i=;i<=n;i++){
cin>>buf[i];
cur=i;
for(int j=;j<;j++){
trie.insert(buf[i]+j); //用每个母串的所有后缀建Trie树,然后利用Trie树对前缀字符串进行操作的特性,达到对母串字符区间的操作
}
}
for(int i=;i<=n;i++){
int le=,ri=,mn=INF;
for(int j=;j<;j++){
int tmp=trie.query(buf[i]+j);
if(tmp!=- && mn>tmp){ //找到每个母串中符合条件的最短子串
mn=tmp;le=j,ri=j+tmp;
}
}
for(int j=le;j<=ri;j++)cout<<buf[i][j];
puts("");
}
}

2019-02-01

Codeforces 861D - Polycarp's phone book 【Trie树】的更多相关文章

  1. Codeforces 615C Running Track(DP + Trie树)

    题目大概说给两个串,问最少要用多少个第一个串的子串(可以翻转)拼成第二个串. UVa1401,一个道理..dp[i]表示前缀i拼接成功所需最少的子串,利用第一个串所有子串建立的Trie树往前枚举转移. ...

  2. Codeforces 633C Spy Syndrome 2 【Trie树】+【DFS】

    <题目链接> 题目大意:给定一个只有小写字母组成的目标串和m个模式串(里面可能有大写字母),记目标串反过来后的串为S,让你从m个模式串中选出若干个组成S串(不区分大小写).输出任意一种方案 ...

  3. Codeforces 514C Watto and Mechanism 【Trie树】+【DFS】

    <题目链接> 题目大意:输入n个单词构成单词库,然后进行m次查询,每次查询输入一个单词(注意这些单词只由a,b,c构成),问该单词库中是否存在与当前查询的单词有且仅有一个字符不同的单词. ...

  4. Codeforces 861D - Polycarp's phone book

    861D - Polycarp's phone book 思路:用map做的话,只能出现一次循环,否则会超时. 代码: #include<bits/stdc++.h> using name ...

  5. codeforces 706D D. Vasiliy's Multiset(trie树)

    题目链接: D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input ...

  6. trie树 Codeforces Round #367 D Vasiliy's Multiset

    // trie树 Codeforces Round #367 D Vasiliy's Multiset // 题意:给一个集合,初始有0,+表示添加元素,-去除元素,?询问集合里面与x异或最大的值 / ...

  7. Codeforces 633C Spy Syndrome 2 | Trie树裸题

    Codeforces 633C Spy Syndrome 2 | Trie树裸题 一个由许多空格隔开的单词组成的字符串,进行了以下操作:把所有字符变成小写,把每个单词颠倒过来,然后去掉单词间的空格.已 ...

  8. Codeforces 514 D R2D2 and Droid Army(Trie树)

    题目链接 大意是判断所给字符串组中是否存在与查询串仅一字符之差的字符串. 关于字符串查询的题,可以用字典树(Trie树)来解,第一次接触,做个小记.在查询时按题目要求进行查询. 代码: #define ...

  9. Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并

    D. Acyclic Organic Compounds   You are given a tree T with n vertices (numbered 1 through n) and a l ...

随机推荐

  1. STM32L476应用开发之三:串行通讯实验

    在我们的项目需求中,有两个串口应用需求,一个是与炭氢传感器的通讯,另一个是与显示屏的通讯.鉴于此,我们需要实验串行通讯. 1.硬件设计 串行通讯一个采用RS232接口,另一个直接采用TTL方式.我们在 ...

  2. C# TTS 文字和英文

    using System;using System.Globalization;using System.Linq;using System.Speech.Synthesis;using System ...

  3. SpringMVC类型转换,验证

    点击上一章-SpringMVC视图及REST风格 Spring mvc 数据绑定流程: SpringMvc将ServletRequest对象及目标方法的形参实例传给WebDataBinderFacto ...

  4. Dig skipfish proxystrike

    1.DNS域名信息收集,(Dig,挖掘局域网的信息之前的博客中已经做过介绍) 查询需要认证的域名服务器  dig -t ns    +   网址 使用工具 fierce 判断主机上存活的其他域名服务器 ...

  5. git使用diff----git-pull之后如何查看拉下来的文件有那些修改

    git pull拉取 git pull对于拉下来的修改文件自动对其进行git add /rm 及git commit 操作.所以拉下来的文件有那些修改,查看的方式可把它们归结于上一次提交的比较. gi ...

  6. Vue-cli 创建的项目配置跨域请求(通过反向代理)---配置多个代理--axios请求

    问题描述: 使用 Vue-cli 创建的项目,开发地址是 localhost:8080,需要访问 localhost:9000 或https://m.maoyan.com或http://image.b ...

  7. mysql的基础知识

    一.存储引擎 mysql> show engines; +--------------------+---------+------------------------------------- ...

  8. SQL查询数据并插入新表

    SQL查询数据并插入新表 --如果接受数据导入的表已经存在 insert into 表 select * from tablename --如果导入数据并生成表 select * into 表 fro ...

  9. KnockoutJs学习笔记(三)

    之前的文章主要针对的是单一的observable(即便是observableArray也是一种observable),而文档Using computed observables则主要是针对ko.com ...

  10. 最新版谷歌浏览器的Flash设置已经不能保存了?

    解决方法:先去chrome实验室界面chrome://flags/#enable-ephemeral-flash-permission选择取消Disabled.取消该实验室选项. 然后去chrome: ...