leetcode336. 回文对(字典树)
给定一组 互不相同 的单词, 找出所有不同 的索引对(i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串。
示例 1:
输入:[“abcd”,“dcba”,“lls”,“s”,“sssll”]
输出:[[0,1],[1,0],[3,2],[2,4]]
解释:可拼接成的回文串为 [“dcbaabcd”,“abcddcba”,“slls”,“llssssll”]
代码
class Solution {class node{int[] next=new int[26];int flag;public node(){flag=-1;}}ArrayList<node> tree=new ArrayList<>();public void insert(String s,int id){int n=s.length(),cur=0;for(int i=0;i<n;i++){int t=s.charAt(i)-'a';if(tree.get(cur).next[t]==0)//不存在就新建{tree.add(new node());tree.get(cur).next[t]=tree.size()-1;}cur=tree.get(cur).next[t];//迭代}tree.get(cur).flag=id;//给最后一个字符加上id}public int search(String s,int l,int r){int cur=0;for(int i=r;i>=l;i--){int t=s.charAt(i)-'a';if(tree.get(cur).next[t]==0)//找不到return -1;cur=tree.get(cur).next[t];}return tree.get(cur).flag;//返回字符串结尾的标志}public boolean isPa(String s,int l,int r)//判断回文串{int len=r-l+1;for(int i=0;i<len/2;i++){if(s.charAt(l+i)!=s.charAt(r-i))return false;}return true;}public List<List<Integer>> palindromePairs(String[] words) {tree.add(new node());List<List<Integer>> res=new ArrayList<>();int n=words.length;for(int i=0;i<n;i++)insert(words[i],i);for(int i=0;i<n;i++){int m=words[i].length();for(int j=0;j<=m;j++)//搜索符合条件的字符串{if(isPa(words[i],j,m-1))//字符串的右部分回文,需要补上与左部分对称的回文字符串{int fl=search(words[i],0,j-1);if(fl!=-1&&fl!=i)//找到了符合的字符串{res.add(Arrays.asList(i,fl));}}if(j>0&&isPa(words[i],0,j-1)){int fr=search(words[i],j,m-1);if(fr!=-1&&fr!=i){res.add(Arrays.asList(fr,i));}}}}return res;}
}
leetcode336. 回文对(字典树)相关推荐
- HDU - 5394 Trie in Tina Town(回文自动机+字典树)
题目链接:点击查看 题目大意:给出一个字典树,现在需要求出字典树上所有的回文串做出的贡献,为 出现次数*回文串长度,求出这个答案 题目链接:可以直接在字典树上dfs然后维护贡献,不过这就涉及到了回文自 ...
- [Leetcode][第336题][JAVA][回文对][暴力][HashSet][字典树]
[问题描述][困难] [解答思路] 1. 暴力(超时) 时间复杂度:O(n 2 ×m),其中 n 是字符串的数量,m 是字符串的平均长度 空间复杂度:O(1) class Solution {publ ...
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest G The Problem to Slow Down You 回文树
The Problem to Slow Down You Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjud ...
- 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities
Palindromes and Super Abilities Problem's Link: http://acm.timus.ru/problem.aspx?space=1&num=1 ...
- 回文树 / 自动机模板
const int maxn = 400000; const int N = 26 ;struct Palindromic_Tree {int next[maxn][N] ;//next指针,next ...
- Palindromic Tree——回文树【处理一类回文串问题的强力工具】
今天我们来学习一个神奇的数据结构:Palindromic Tree.中译过来就是--回文树. 那么这个回文树有何功能? 假设我们有一个串S,S下标从0开始,则回文树能做到如下几点: 1.求串S前缀0~ ...
- 回文串问题的克星——Palindrome Tree(回文树)/Palindrome Automaton(回文自动机)学习小记
前言 最近B组有一道我不会的题,赶紧学习. 简介 我们知道,Manacher算法可以在 O(n) O ( n ) O(n)的时间内求出以每个位置为中心的最长回文串(虽然我昨天还不知道Manacher算 ...
- 回文树(模板)+ 例题
引用: Palindromic Tree--回文树[处理一类回文串问题的强力工具] 回文树练习题集 首先,回文树有何功能? 假设我们有一个串S,S下标从0开始,则回文树能做到如下几点: 1.求串S前缀 ...
- Palindromic Tree——回文树(回文自动机)
Palindromic Tree.中译过来就是--回文树. 那么这个回文树有何功能? 假设我们有一个串S,S下标从0开始,则回文树能做到如下几点: 1.求串S前缀0~i内本质不同回文串的个数(两个串长 ...
最新文章
- [原创]CentOS下Mysql双机互为备份
- 033_使用ArrayDeque模拟队列结构
- IE 11 和360浏览器不能安装网银控件
- 当 TiDB 与 Flink 相结合:高效、易用的实时数仓
- javascript 学习笔记之面向对象编程(二):继承多态
- 用栈来求解汉诺塔变形问题
- sklearn自学指南(part17)--稳健回归-异常值和建模误差
- mysql同表字段前4位复制_MySQL不同表之前的字段复制
- 如何清除web 服务器的缓存
- Mybatis ResultMap Collection 复合主键
- 购书网站前端实现(HTML+CSS+JavaScript)
- 双网卡上网冲突解决_交换机如何设置,控制IP地址冲突故障
- java以及JavaScript的香港身份证验证方法。
- 在网页中使用嵌入字体
- 大数据的监控和数据准确性保证
- 简单解决高分屏模糊问题
- 深度学习方法(二十一):常用权重初始化方法Xavier,He initialization的推导
- 电子宠物小狗-内部结构是什么?
- windows workflow foundation(WWF)学习记录
- Linux CFS中的进程调度
热门文章
- C++设计模式之策略模式(Strategy)
- 7天拿到阿里安卓岗位offer,附高频面试题合集
- ThreadPoolExcutor 线程池 异常处理 (上篇)
- Hadoop 倒排索引
- datatable转化泛型
- SVN中Revert changes from this revision 跟Revert to this revision
- javascript之 原生document.querySelector和querySelectorAll方法
- Android 手把手带你玩转自己定义相机
- WPF Slider设置整数
- Canvas createImageData