POJ3630
Tire树裸题,一开始写动态的字典树,然后TLE,每次new一个新节点耗费时间较多。后来改成数组模拟的。
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std ;
const int maxN = ; struct stringStruct {
char str[ ] ;
int len ;
}ss[ maxN ] ; struct tireNode {
int cnt ;
bool last ;
struct tireNode *next[ ] ;
tireNode ( ) {
cnt = ;
last = false;
memset ( next , , sizeof ( next ) ) ;
}
}; bool cmp ( stringStruct a , stringStruct b ) {
return a.len < b.len ;
} bool insert ( tireNode *root , char str[ ] ) {
int len = strlen ( str ) ;
tireNode *cur = root ; for ( int i= ; i<len ; ++i ) {
char ch = str[ i ] - '' ;
if ( cur -> next[ ch ] == NULL ) {
tireNode* newNode = new tireNode ;
cur -> next[ ch ] = newNode ;
}
if ( cur -> last ) return true ;
cur = cur -> next[ ch ] ;
cur->cnt++ ;
}
cur -> last = true ;
return false ;
} int main ( ) {
int T ;
for ( scanf( "%d" , &T ) ; T ; --T ) {
tireNode* root = new tireNode ;
int N ;
scanf ( "%d\n" , &N ) ;
for ( int i= ; i<=N ; ++i ) {
char ch = getchar ( ) ;
int lenn = ;
while ( ch != '\n' ) {
ss[ i ].str[ lenn++ ] = ch ;
ch = getchar ( ) ;
}
ss[ i ].len = lenn ;
}
sort ( ss + , ss + N + , cmp ) ;
bool flag = true ;
for ( int i= ; i<=N ; ++i ) {
if ( insert ( root , ss[ i ].str ) ) {
flag = false ;
break ;
}
}
if ( flag ) printf ( "YES\n" ) ;
else printf ( "NO\n" ) ;
}
return ;
}
TLE动态字典树
#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std ;
const int maxN = ;
typedef long long LL ; struct stringStruct {
char str[ ] ;
int len ;
}ss[ maxN ] ; int trie[ maxN ][ ] ;
bool end[ maxN ] ;
bool cmp ( stringStruct a , stringStruct b ) {
return a.len < b.len ;
} int _cnt ; bool insert ( char str[ ] ) {
int cur = , len = strlen ( str + ) ;
for ( int i= ; i<=len ; ++i ) {
int ch = str[ i ] - '' ;
if ( !trie[ cur ][ ch ] )
trie[ cur ][ ch ] = ++_cnt ;
if ( end[ cur ] ) return true ;
cur = trie[ cur ][ ch ] ;
}
end[ cur ] = true ;
return false ;
} int main ( ) {
int T , N ;
for ( scanf ( "%d" , &T ) ; T ; --T ) {
memset ( trie , , sizeof ( trie ) ) ;
memset ( end , false , sizeof ( end ) ) ;
_cnt = ;
scanf ( "%d" , &N ) ;
for ( int i= ; i<=N ; ++i ) {
scanf ( "%s" , ss[ i ].str + ) ;
ss[ i ].len = strlen ( ss[ i ].str + ) ;
}
sort ( ss + , ss + N + , cmp ) ;
bool flag = true ;
for ( int i= ; i<=N ; ++i ) {
if ( insert ( ss[ i ].str ) ) {
flag = false ;
break ;
}
}
if ( flag )printf ( "YES\n" ) ;
else printf ( "NO\n" ) ;
}
return ;
}
AC数组模拟
POJ3630的更多相关文章
- POJ--1056 IMMEDIATE DECODABILITY && POJ--3630 Phone List(字典树)
题目链接 题目大意 看输入的每个字符串中是否有一个字符串是另一个字符串的前缀 #include<iostream> #include<cstring> #include< ...
- POJ1056 IMMEDIATE DECODABILITY & POJ3630 Phone List
题目来源:http://poj.org/problem?id=1056 http://poj.org/problem?id=3630 两题非常类似,所以在这里一并做了. 1056题目大意: 如果一 ...
- POJ3630/HDU-1671 Phone List,字典树静态建树!
Phone List POJ动态建树TLE了~~~ 题意:拨打某个电话时可能会因为和其他电话号码的前几位重复而导致错误,现在给出一张电话单,求是否有某个电话是其他电话的前缀.是则输出NO,否则输出YE ...
- Phone List POJ-3630 字典树 or 暴力
Phone List POJ-3630 字典树 or 暴力 题意 目前有 t 组数据, n 个电话号码,如果拨打号码的时候 先拨通了某个号码,那么这一串号码就无法全部拨通. 举个例子 911 和 91 ...
- HihoCoder第二周与POJ3630:Trie树的建立
这又是两道一样的题,都是建立trie树的过程. HihoCoder第二周: 这里其实逻辑都很简单,主要在于数据结构struct的使用. #include <iostream> #inclu ...
- hdu杭电1671 / poj3630 字典树
传送门 题意:输入n串数字 找出是否有存在串的前缀与另一个串相同 如果存在 输出NO否则输出YES 思路:用字典树解决 标记字典树总串的结尾 查找出一个串内部是否有被标记的节点 如果有那么说明存在前缀 ...
- POJ3630——简单Trie树
这个题的意思是说,给出一些字符串,判断是否有字符串是另一个字符串的前缀,当然可以用排序水过,不过这个题拿来练习一下Trie树不错. 这个题在poj的discuss上好多人说必须要静态建树,估计都是用了 ...
- [POJ3630]Phone List (Tire)
题意 trie字典树模板 LOJ有中文翻译https://loj.ac/problem/10049 思路 TIRE 代码 之前在LOJ上做过 直接交了 #include<cstdio> # ...
- poj3630 Phone List
spy on一下,发现是trie裸题,结果一交就T... 然后把cin改成scanf就A了... trie的空间一定要开足,要不然RE #include <cstdio> #include ...
随机推荐
- avg 的使用
select * from emp where sal>(select avg(sal) as ssalfrom emp);--要求查询出高于公司平均工资的全部雇员信息
- 更改MySQL密码
#安装MySQL5.7参考:https://blog.csdn.net/qq_23033339/article/details/80872136#MYSQL的基础操作参考:https://www.cn ...
- MySQL命令行查询乱码解决方法
转自Agoly的博客,原文链接https://www.cnblogs.com/qmfsun/p/4846467.html 感谢博主Agoly这篇文章说的很详细很透彻. MySQL会出现中文乱码的原因不 ...
- SimpleDateFormat日期格式解析
先看一个代码示例: import java.text.SimpleDateFormat; import java.util.Date; public class test{ public static ...
- iOS 单选框
iOS 单选框,可自定义横向和纵向显示,可定义显示的个数和内容,自定义间距,提供block 和代理方法可供使用,欢迎拍砖! github地址: https://github.com/joshuaGen ...
- pythonのdjango Form简单应用。
Form表单有两种应用场景: 1.生成HTML标签. 2.验证输入内容. 如果我们在django程序中使用form时,需要在views中导入form模块 from django import form ...
- Git(1):版本库+工作区+暂存区
参考博客:https://blog.csdn.net/qq_27825451/article/details/69396866
- 使用Setup factory打包WPF
软件环境 Win10 .NET452 WPF Setup Factory 工具直接百度下啦,关键词:Setup Factory 95 With Sn 打包过程主要参考了以下文章: https://ww ...
- Mysql 反向解析 导致远程访问慢
在云端部署了mysql后,发现远程连接的响应速度非常慢(3-10s) 但是在本地访问数据库却没有问题 经过一番google这才知道原来mysql默认会进行反向解析,即通过ip地址反向向ISP申请获取域 ...
- 2-1、FileBeat入门
FileBeat入门 要开始使用Filebeat设置,请安装并配置相关产品: 用于存储和索引数据的Elasticsearch. 用户界面的Kibana. 用于解析和增强数据的Logstash(可选). ...