Codeforces 159D Palindrome pairs
http://codeforces.com/problemset/problem/159/D
题目大意:
给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数。
思路:num[i]代表左端点在i这个位置的回文串个数,然后用树状数组维护sum[i],代表回文串右端点小于等于i的回文串数,总复杂度:O(n^2)
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#define ll long long
ll c[],num[];
int pd[][],n;
char s[];
int lowbit(int x){
return x&(-x);
}
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void add(int x){
for (int i=x;i<=n;i+=lowbit(i)){
c[i]++;
}
}
int ask(int x){
int res=;
for (int i=x;i;i-=lowbit(i)){
res+=c[i];
}
return res;
}
int main(){
scanf("%s",s+);
n=strlen(s+);
for (int i=;i<=n;i++)
pd[i][i]=,add(i),num[i]++;
for (int i=;i<n;i++)
if (s[i]==s[i+]) pd[i][i+]++,add(i+),num[i]++;
for (int len=;len<=n;len++)
for (int i=;i+len-<=n;i++){
int j=i+len-;
if (pd[i+][j-]&&s[i]==s[j]) pd[i][j]=,add(j),num[i]++;
}
ll ans=;
for (int i=;i<=n;i++)
ans+=ask(i-)*((ll)(num[i]));
printf("%I64d\n",ans);
return ;
}
Codeforces 159D Palindrome pairs的更多相关文章
- codeforces 1045I Palindrome Pairs 【stl+构造】
题目:戳这里 题意:给1e5个字符串,问有多少对字符串组合,满足最多只有一种字符有奇数个. 解题思路:每种情况用map存一下就行了.感觉这题自己的代码思路比较清晰,所以写个题解记录一下 附ac代码: ...
- DP VK Cup 2012 Qualification Round D. Palindrome pairs
题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...
- Codeforces 486C Palindrome Transformation(贪心)
题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...
- LeetCode 336. Palindrome Pairs
原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...
- 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 ...
- 【题解】Palindrome pairs [Codeforces159D]
[题解]Palindrome pairs [Codeforces159D] 传送门:\(Palindrome\) \(pairs\) \([CF159D]\) [题目描述] 给定一个长度为 \(N\) ...
- leetcode 132 Palindrome Pairs 2
lc132 Palindrome Pairs 2 大致与lc131相同,这里要求的是最小分割方案 同样可以分割成子问题 dp[i][j]还是表示s(i~j)是否为palindrome res[i]则用 ...
- leetcode 131 Palindrome Pairs
lc131 Palindrome Pairs 解法1: 递归 观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome 那么挑选cut的位置就很有意思,后一次cut可以建立在前一 ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
随机推荐
- android 在EditText中显示表情图片
public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { su ...
- SQL-Customers Who Never Order
Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL qu ...
- 多线程面试题(Google)
有四个线程1.2.3.4.线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD.初始都为空.现要让四个文件呈如下格式: A:1 2 3 4 1 2.... ...
- Eclipse Tomcat : Project facet Java version 1.7 is not supported.
在Eclipse打开一个项目,并用Tomcat运行时,报错:Project facet Java version 1.7 is not supported. 大致的截图如下: "项目中的jd ...
- struts2.1.3之后使用自定义Filter
struts2中 ActionContextCleanUp, StrutsPrepareAndExecuteFilter, StrutsPrepareFilter,StrutsExecuteFilte ...
- 简单的通过NSFileManager 存储图片
UIImage *image = [UIImage imageNamed:@"Default.png"]; NSData *data = UIImageJPEGRepresenta ...
- 学习手机游戏开发的两个方向 Cocos2d-x 和 Unity 3D/2D,哪个前景更好?
如题! 首先说一说学习手机游戏(移动游戏)这件事. 眼下移动互联网行业的在以井喷状态发展.全球几十亿人都持有智能终端设备(ios android),造就了非常多移动互联网创业机会: 一.移动社交 微信 ...
- android屏蔽状态栏显示
framework/base/packages/SystemUI/res/layout/status_bar_expanded.xml 要屏蔽哪个设置哪个属性为: android:visibility ...
- MongoDB Connector for Hadoop
MongoDB Connector for Hadoop https://github.com/mongodb/mongo-hadoop Purpose The MongoDB Connector f ...
- [React] React Router: Router, Route, and Link
In this lesson we'll take our first look at the most common components available to us in react-rout ...