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 相似题目 参考资料 日期 题目地 ...
随机推荐
- left join 关联条件位置
select e.last_name, e.department_id, d.department_name from hr.employees e left outer join hr.depart ...
- 求一个数组中第K小的数
面试南大夏令营的同学说被问到了这个问题,我的第一反应是建小顶堆,但是据他说用的是快排的方法说是O(n)的时间复杂度, 但是后来经过我的考证,这个算法在最坏的情况下是O(n^2)的,但是使用堆在一般情况 ...
- [置顶] API相关工作过往的总结之整体介绍
此系列的总结文章,仅仅是我个人工作总结,有考虑不周之处还请各位同行多多指教. API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是 ...
- MVC框架个人浅析
1.概述: 相信不少前端从业者,都会遇到MVC编程模式,现今多数轻量级网站用php作为后台交互,MVC编程模式用于JAVAWEB开发,应用类型以系统占多数(包括并不限于系统,电商网站,平台交互当然都能 ...
- easyui 获取cloumns字段
var colums=datagrid.datagrid('options').columns; var frozens=datagrid.datagrid('options').frozenColu ...
- C# - CSV(Comma-Separated Values)文件读取.
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Sys ...
- arcgis server manager - An error has occured on the server. For details please check the Event (Application) log on the web.
当登陆 Arcgis Server Manager的时候,点击 "Services" 中的选项"Manage Services",就报错: An error h ...
- [Leetcode][048] Rotate Image 略详细 (Java)
题目在这里 https://leetcode.com/problems/rotate-image/ [个人分析] 这个题目,我觉得就是考察基本功.考察细心的,算法方面没有太多东西,但是对于坐标的使用有 ...
- PHP echo, print, printf, sprintf函数的区别和使用
1. echo函数: 输出函数,是命令,不能返回值.echo后面可以跟很多个参数,之间用分号隔开,如: echo $myvar1; echo 1,2,$myvar,"<b>bol ...
- JavaScript高级程序设计第20章JSON 笔记 (学习笔记)
第二十章 JSON 1.Json 可以表示三种类型的值: 1.简单值: 表示数值:5 表示字符串:“hello wrold”注表示字符串时必须使用双引号 2.对象: {“name”:“mi”,”ag ...