题目链接:link

首先上思路:

如果一个字符串是回文串,只有当其中最多的只有一个字符的出现次数是奇数。

注意我们可以将每个字符串的字符出现次数的奇偶性用一个 \(26\) 位的二进制表示。

接下来就是是回文对的条件:

我们分为 \(2\) 种情况。

第一种情况:

两个字符串的二进制表示完全相同。那么也就是两个字符串的字符出现次数的奇偶性完全相同,所以拼接后的字符串中每个字符的出现次数都是偶数,所以它们可以构成回文串。

第二种情况:

两个字符串的二进制表示只有一位不相同。那么也就是拼接后的字符串也只有一个字符出现次数是奇数,所以它们也可以构成回文串。

最后就是统计回文串了!

我们可以使用哈希表 mp 用来记录每个二进制表示的出现次数。然后用 ct 来记录每个二进制表示可以匹配的次数。

那么最后的答案就是两种情况的和。

如果还不懂看看代码吧!

#include<bits/stdc++.h>
#define I using
#define AK namespace
#define IOI std
#define i_ak return
#define ioi 0
#define i_will signed
#define ak main
#define IMO ()
#define int long long
I AK IOI;
/*mp 记录每个二进制表示的出现次数,ct 记录每个二进制表示可以被匹配的次数*/
unordered_map<int,int>mp,ct;
vector<int>v;//存储所有二进制表示
/*n 表示字符串数量,x 表示当前字符串的二进制表示,res 代表结果*/
int n,x,res;
i_will ak IMO{
cin>>n;
for(int e=1;e<=n;e++){
x=0;
string s;
cin>>s;
for(int i=0;i<s.size();i++)x^=(1<<(s[i]-'a'));//将字符串奇偶性记录到二进制表示中
v.push_back(x);
for(int i=0;i<26;i++)if(((x>>i)&1)==0)ct[(x+(1<<i))]++;//记录当前二进制表示仅有一位不同
mp[x]++;
}
for(auto e:mp)res+=e.second*(e.second-1)/2;
for(auto t:v)res+=ct[t];
cout<<res;
i_ak ioi;
}

时间复杂度:\(O(N\times L)\),其中 \(L\) 是字符串的平均长度。

空间复杂度:\(O(N)\)。

亲测可过,请勿抄袭!

题解:CF1045I Palindrome Pairs的更多相关文章

  1. 【题解】Palindrome pairs [Codeforces159D]

    [题解]Palindrome pairs [Codeforces159D] 传送门:\(Palindrome\) \(pairs\) \([CF159D]\) [题目描述] 给定一个长度为 \(N\) ...

  2. LeetCode 336. Palindrome Pairs

    原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...

  3. DP VK Cup 2012 Qualification Round D. Palindrome pairs

    题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...

  4. 336. Palindrome Pairs(can't understand)

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

  5. leetcode 132 Palindrome Pairs 2

    lc132 Palindrome Pairs 2 大致与lc131相同,这里要求的是最小分割方案 同样可以分割成子问题 dp[i][j]还是表示s(i~j)是否为palindrome res[i]则用 ...

  6. leetcode 131 Palindrome Pairs

    lc131 Palindrome Pairs 解法1: 递归 观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome 那么挑选cut的位置就很有意思,后一次cut可以建立在前一 ...

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

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

  8. [LeetCode] Palindrome Pairs 回文对

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

  9. 【LeetCode】Palindrome Pairs(336)

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

  10. Palindrome Pairs -- LeetCode 336

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

随机推荐

  1. Huawei Cloud EulerOS上安装sshpass

    下载源码 git clone https://github.com/kevinburke/sshpass.git 由于网络问题,这里我用了一个代理下载 git clone https://ghprox ...

  2. linux部署go项目

    直接部署: 1.将程序所需要的文件如配置文件和生成的可执行文件拷贝到linux中 2.直接执行./main命令,启动程序 (main是go编译生成的可执行文件) 如果报Permission denie ...

  3. OSPF协议报文

    OSPF(Open Shortest Path First,开放最短路径优先)是一种内部网关协议(Interior Gateway Protocol,IGP),用于在同一个自治系统(Autonomou ...

  4. 数据库MVCC详解

    MVCC 1.基本介绍 数据库:MySQL.[很多主流数据库都使用了MVCC,比如MySQL的InnoDB引擎.PostgreSQL.Oracle] MVCC,全称Multi-Version Conc ...

  5. AI穿上身:苹果手表如何改变你的生活?

    楔子:一个普通理工男的科技启示录 我是张三,一个标准的90后理工男.在这个日新月异的科技时代,我习惯用精密的逻辑和近乎机械的效率来审视世界.每天早上6点45分准时起床,每一分钟都被精确地规划,生活就像 ...

  6. 【SpringMVC】视图和视图解析器

    视图和视图解析器 Spring MVC如何解析视图 视图和视图解析器 请求处理方法执行完成后,最终返回一个 ModelAndView对象.对于那些返回 String,View 或 ModeMap 等类 ...

  7. 文字像素(.NET)

    无图言* 代码实现 新建一个控制台应用程序, 调整 Program.cs 文件内容如下: using System; using System.Drawing; namespace ConsoleAp ...

  8. 如果在安装32位Oracle客户端组件的情况下64位模式运行, 将出现此问题.

    场景重现 在一台Windows 7 32-bit电脑上 安装了Oracle 11gR2 32-bit的客户端 用 VS2010 写的一个基于数据库驱动的项目 操作Oracle数据库都挺正常的 后来.. ...

  9. C# 中合并2个 Dictionary

    内置方法 using System.Collections.Generic; using System.Linq; Dictionary<string, object> dicA = ne ...

  10. 文件导出ZIP压缩

    文件导出ZIP压缩 @Override public String downloadallfiles(HttpServletRequest request, String lsbpId, String ...