Codeforces 159D Palindrome pairs

http://codeforces.com/problemset/problem/159/D

题目大意:

给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数。

思路:num[i]代表左端点在i这个位置的回文串个数,然后用树状数组维护sum[i],代表回文串右端点小于等于i的回文串数,总复杂度:O(n^2)

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<iostream>
 6 #define ll long long
 7 ll c[200005],num[200005];
 8 int pd[2005][2005],n;
 9 char s[200005];
10 int lowbit(int x){
11     return x&(-x);
12 }
13 int read(){
14     int t=0,f=1;char ch=getchar();
15     while (ch<‘0‘||ch>‘9‘){if (ch==‘-‘) f=-1;ch=getchar();}
16     while (‘0‘<=ch&&ch<=‘9‘){t=t*10+ch-‘0‘;ch=getchar();}
17     return t*f;
18 }
19 void add(int x){
20     for (int i=x;i<=n;i+=lowbit(i)){
21         c[i]++;
22     }
23 }
24 int ask(int x){
25     int res=0;
26     for (int i=x;i;i-=lowbit(i)){
27         res+=c[i];
28     }
29     return res;
30 }
31 int main(){
32     scanf("%s",s+1);
33     n=strlen(s+1);
34     for (int i=1;i<=n;i++)
35      pd[i][i]=1,add(i),num[i]++;
36     for (int i=1;i<n;i++)
37      if (s[i]==s[i+1]) pd[i][i+1]++,add(i+1),num[i]++;
38     for (int len=3;len<=n;len++)
39      for (int i=1;i+len-1<=n;i++){
40         int j=i+len-1;
41         if (pd[i+1][j-1]&&s[i]==s[j]) pd[i][j]=1,add(j),num[i]++;
42      }
43     ll ans=0;
44     for (int i=1;i<=n;i++)
45       ans+=ask(i-1)*((ll)(num[i]));
46     printf("%I64d\n",ans);
47     return 0;
48 }
时间: 2024-10-27 01:57:10

Codeforces 159D Palindrome pairs的相关文章

codeforces 1045I Palindrome Pairs 【stl+构造】

题目:戳这里 题意:给1e5个字符串,问有多少对字符串组合,满足最多只有一种字符有奇数个. 解题思路:每种情况用map存一下就行了.感觉这题自己的代码思路比较清晰,所以写个题解记录一下 附ac代码: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 2e5 + 10; 5 const ll mod = 998244353; 6 int arr[33]; 7 i

codeforces159D - Palindrome pairs 二重DP

题意:给你一个字符串,问你其中不重叠的回文字串对有多少 解题思路:这题用到两种方法,不过其实是一个很巧妙的二重dp 1)暴力求解以i开头,j结尾的是否为回文,如果是,ans += sum[i-1](ans 为答案, sum[i]为在  1 - i内回文串的个数--需要dp求解) 这里非常耗时,时间大约为  n^3 ,  跑出来为 830ms 解题代码: 1 // File Name: 159d.cpp 2 // Author: darkdream 3 // Created Time: 2014年

[LeetCode]Palindrome Pairs

题目:Palindrome Pairs Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome. Example 1:Given words = ["bat", "tab",

Codeforces 486C Palindrome Transformation(贪心)

题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N,指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:其实只要是对称位置不相同的,那么指针肯定要先移动到这里,修改字符只需要考虑两种方向哪种更优即 可.然后将所有需要到达的位置跳出来,贪心处理. #include <cstdio> #include <cstring> #include <cstdlib> #include <vec

DP VK Cup 2012 Qualification Round D. Palindrome pairs

题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 1 /* 2 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 3 DP:很巧妙的从i出发向两头扩展判断是否相同来找回文串 4 dpr[i] 代表记录从0到i间的回文子串的个数,dpl[i] 代表记录i之后的回文子串个数 5 两两相乘就行了 6 详细解释:http://blog.csdn.net/shiyuankongbu/article/details

336. Palindrome Pairs

Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e.words[i] + words[j] is a palindrome. Example 1:Given words = ["bat", "tab", "cat"]Retu

Palindrome Pairs

Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e.words[i] + words[j] is a palindrome. Example 1:Given words = ["bat", "tab", "cat"]Retu

[LeetCode] Palindrome Pairs 回文对

Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome. Example 1:Given words = ["bat", "tab", "cat"]Ret

【Leetcode】Palindrome Pairs

题目链接:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e.words[i] + words[j] is a palindrome. Example 1: Given