给定一组独特的单词, 找出在给定列表中不同 的索引对(i, j),使得关联的两个单词,例如:words[i] + words[j]形成回文。
示例 1:
给定 words = ["bat", "tab", "cat"]
返回 [[0, 1], [1, 0]]
回文是 ["battab", "tabbat"]
示例 2:
给定 words = ["abcd", "dcba", "lls", "s", "sssll"]
返回 [[0, 1], [1, 0], [3, 2], [2, 4]]
回文是 ["dcbaabcd", "abcddcba", "slls", "llssssll"]

详见:https://leetcode.com/problems/palindrome-pairs/description/

C++:

  1. class Solution {
  2. public:
  3. vector<vector<int>> palindromePairs(vector<string>& words) {
  4. vector<vector<int>> res;
  5. unordered_map<string, int> m;
  6. set<int> s;
  7. for (int i = 0; i < words.size(); ++i)
  8. {
  9. m[words[i]] = i;
  10. s.insert(words[i].size());
  11. }
  12. for (int i = 0; i < words.size(); ++i)
  13. {
  14. string t = words[i];
  15. int len = t.size();
  16. reverse(t.begin(), t.end());
  17. if (m.count(t) && m[t] != i)
  18. {
  19. res.push_back({i, m[t]});
  20. }
  21. auto a = s.find(len);
  22. for (auto it = s.begin(); it != a; ++it)
  23. {
  24. int d = *it;
  25. if (isValid(t, 0, len - d - 1) && m.count(t.substr(len - d)))
  26. {
  27. res.push_back({i, m[t.substr(len - d)]});
  28. }
  29. if (isValid(t, d, len - 1) && m.count(t.substr(0, d)))
  30. {
  31. res.push_back({m[t.substr(0, d)], i});
  32. }
  33. }
  34. }
  35. return res;
  36. }
  37. bool isValid(string t, int left, int right)
  38. {
  39. while (left < right)
  40. {
  41. if (t[left++] != t[right--])
  42. {
  43. return false;
  44. }
  45. }
  46. return true;
  47. }
  48. };

参考:https://leetcode.com/problems/palindrome-pairs/description/

336 Palindrome Pairs 回文对的更多相关文章

  1. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  2. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  3. LeetCode Valid Palindrome 有效回文(字符串)

    class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...

  4. CF932G Palindrome Partition(回文自动机)

    CF932G Palindrome Partition(回文自动机) Luogu 题解时间 首先将字符串 $ s[1...n] $ 变成 $ s[1]s[n]s[2]s[n-1]... $ 就变成了求 ...

  5. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. [LeetCode] 214. Shortest Palindrome 最短回文串

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. 【LeetCode】336. Palindrome Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  8. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  9. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

随机推荐

  1. uva 1444 Knowledge for the masses

    uva 1444 Description   You are in a library equipped with bookracks that move on rails. There are ma ...

  2. Codeforces 990D - Graph And Its Complement

    传送门:http://codeforces.com/contest/990/problem/D 这是一个构造问题. 构造一张n阶简单无向图G,使得其连通分支个数为a,且其补图的连通分支个数为b. 对于 ...

  3. pogresql基础学习笔记

    命令行工具:psql 可视化工具:pgAdmin 查看所有表: 命令行:\d sql:select * from pg_tables WHERE schemaname='public'; 查看表结构: ...

  4. nyoj 55 懒省事的小明(priority_queue优先队列)

    懒省事的小明 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述       小明很想吃果子,正好果园果子熟了.在果园里,小明已经将所有的果子打了下来,而且按果子的不同种 ...

  5. HDU 2242 连通分量缩点+树形dp

    题目大意是: 所有点在一个连通图上,希望去掉一条边得到两个连通图,且两个图上所有点的权值的差最小,如果没有割边,则输出impossible 这道题需要先利用tarjan算法将在同一连通分量中的点缩成一 ...

  6. zoj——1311 Network

    Network Time Limit: 2 Seconds      Memory Limit: 65536 KB A Telephone Line Company (TLC) is establis ...

  7. Mutual Training for Wannafly Union #5

    A(UVA12336) 题意:给一个n*m(n,m<=1e5)的棋盘,棋盘上有一些障碍点不能放棋子,现在要在棋盘上放4个棋子,满足A->B->C->D->A,其中走的规则 ...

  8. BMP的图像处理

    近期碰到了一个问题将图片缩放: 进行了整理发现位图一些主要的结构能够进行整理,得出下面图表: 进行图片缩放的时候会进行一些处理(最临近差值法): 详细的代码例如以下: #include <std ...

  9. 犀牛Phinoceros 如何切换中文语言

    Tools-Options-Rhino Options-Appearance,然后改成中文        

  10. openCV—Python(2)—— 载入、显示和保存图像

    一.函数简单介绍 1.imread-读取图像 函数原型:imread(filename, flags=None) filename:读取的图像路径名:比如:"H:\img\lena.jpg& ...