cf434 B. Polycarp's phone book
来源:互联网 发布:剑灵龙女免费捏脸数据 编辑:程序博客网 时间:2024/06/05 07:43
题目
题意:
给你一些10位数的串,输出每个串不与其他串共有的子串;
看了别人的博客,加了一些注释。。
//思路:利用字典树把每个串的所有子串枚举出来,再互相比较
#include<cstdio>
#include<iostream>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char str[70010][15];
struct tree
{
tree*next[10];
int num;
tree()
{
memset(next,NULL,sizeof(next));//构造函数,,这种方法感觉很好
num=1;
}
}root;
int st,lent;//记录子串起点和长度
void build(int s,char*str)//构造
{
tree*p=&root;
tree*q;
int len=strlen(str);
for(int i=s;i<len;i++)
{
int id=str[i]-'0';
if(p->next[id]!=NULL)
{
p->next[id]->num++;
p=p->next[id];
}
else
{
q=new tree;
p->next[id]=q;
p=p->next[id];
}
}
}
int findd(int s,char*str)
{
tree*p=&root;
int len=strlen(str);
for(int i=s;i<len;i++)
{
int id=str[i]-'0';
if(p->next[id]==NULL)
return 0;
p=p->next[id];
if(p->num==0)
{
if(i-s<lent)//求最短的不与其他串共有的子串
{
lent=i-s;
st=s;
return 0;
}
}
}
}
void solve1(int s,char*str)
{
tree*p=&root;
int len=strlen(str);
for(int i=s;i<len;i++)
{
int id=str[i]-'0';
if(p->next[id]==NULL)
return;
p=p->next[id];
p->num--;
}
}
void solve2(int s,char*str)//复原每个节点的num值
{
tree*p=&root;
int len=strlen(str);
for(int i=s;i<len;i++)
{
int id=str[i]-'0';
if(p->next[id]==NULL)
return;
p=p->next[id];
p->num++;
}
}
int main()
{
int n;
root.num=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
for(int j=0;str[i][j]!='\0';j++)
build(j,str[i]);
}
for(int i=0;i<n;i++)
{
lent=1e9+7;
for(int j=0;str[i][j]!='\0';j++)
solve1(j,str[i]);
for(int j=0;str[i][j]!='\0';j++)
findd(j,str[i]);
for(int j=0;str[i][j]!='\0';j++)
solve2(j,str[i]);
for(int j=st;j<=st+lent;j++)
printf("%c",str[i][j]);
printf("\n");
}
return 0;
}
- cf434 B. Polycarp's phone book
- Polycarp's phone book
- Codeforces858D Polycarp's phone book
- 858D Polycarp's phone book 字典树
- Codeforces Round #434 Polycarp's phone book (字典树)
- codeforce 858D Polycarp's phone book(字典树)
- codeforces 858D Polycarp's phone book(字典树)
- CodeForces 858D Polycarp's phone book(Trie)
- Codeforces 861 D. Polycarp's phone book (trie)
- Codeforces Round #434 D. Polycarp's phone book (字典树)
- Codeforces Round #434 (Div. 2): D. Polycarp's phone book(字典树)
- Codeforces Round #434 (Div. 2) D Polycarp's phone book(字符串,字典树)
- Codeforces Round #434 (Div. 2, )-字典树&好题&板子-Polycarp's phone book
- CodeForces 858C Did you mean... 、 CodeForces 858D Polycarp's phone book!黑科技
- Codeforces 861 D Polycarp's phone book(字典树模板)
- B. Polycarp and Letters
- B. Polycarp and Letters
- Sunshine’s book(book)
- 破解米开朗基罗-蒋勋有声
- 网页布局总结
- 第三周 项目一 顺序表的基本运算 (1)(2)(3)
- HDU 2100 Lovekey 大数java
- APUE 第13章 守护进程
- cf434 B. Polycarp's phone book
- Kotlin极简教程:第7章 面向对象编程
- PAT 甲级 1017. Queueing at Bank (25)
- Helm简介,安装与使用
- 第二周项目3 -体验复杂度
- HTML DOM prompt() 方法
- HDU 1286 找新朋友
- oracle_并发问题事件解析
- 高二&高一&初三模拟赛17 总结