题目链接:http://codeforces.com/problemset/problem/245/H

题目大意:给你一个字符串s,对于每次查询,输入为一个数对(i,j),输出s[i..j]之间回文串的个数。

容斥原理: dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1];

if( str[i]==str[j] 并且 str[i+1..j-1]是回文串 ) dp[i][j]++;

代码:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <cmath>
#include <numeric>
#include <iterator>
#include <iostream>
#include <cstdlib>
#include <functional>
#include <queue>
#include <stack>
#include <string>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ size()
#define ST begin()
#define ED end()
#define CLR clear()
#define ZERO(x) memset((x),0,sizeof(x))
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const double EPS = 1e-; const int MAX_N = ;
char str[MAX_N];
int dp[MAX_N][MAX_N];
int q; bool IsPalindromes(int i,int j){
if(i>=j) return true;
return dp[i][j] == dp[i+][j]+dp[i][j-]-dp[i+][j-]+;
} int main() {
scanf("%s",str+);
int length = strlen(str+);
// printf("length: %d\n",length);
for(int i=;i<=length;i++) dp[i][i] = ; for(int len = ; len<=length; len++) {
for(int i=;i<=length;i++) {
int j = i+len-;
if( j>length ) continue;
dp[i][j] = dp[i][j-]+dp[i+][j]-dp[i+][j-];
if( str[i]==str[j] ) {
if( IsPalindromes(i+,j-) ) {
dp[i][j] ++ ;
}
}
}
} // for(int i=1;i<=length;i++) {
// for(int j=1;j<=length;j++) {
// printf("%d ",dp[i][j]);
// }
// puts("");
// } scanf("%d",&q); while( q-- ) {
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",dp[l][r]);
} return ;
}

最新文章

  1. Javascript的shift()和push(),unshift()和pop()方法简介
  2. Mifare系列5-存储结构(转)
  3. wxPython--Python GUI编程参考链接
  4. Python进阶07 函数对象
  5. MapReduce编程系列 — 6:多表关联
  6. Fragment 整个生命周期
  7. wxPython Modal Dialog 模式对话框
  8. poj 1259 Agri-Net(最小生成树)
  9. Ext.Net 使用总结之GridPanel中的选中行
  10. ffempg支持文件解码
  11. iota
  12. matplotlib各图形绘制
  13. 003-docker命令-远程镜像仓库命令,本地镜像管理命令
  14. 如何使用DSP的cache(转)
  15. dir for RequestHandler and request
  16. isKindOfClass isMemeberOfClass 的区分
  17. Windows环境下多版本JDK切换
  18. Metapackage包
  19. nodejs基础 -- EventEmitter
  20. RS报内存错误XQE-ROL-0183

热门文章

  1. Node.js 事件
  2. SSL/TLS 高强度加密: 常见问题解答
  3. GPS模块启动模式说明
  4. iOS 图片填充 UIImageView (contentMode)
  5. Redisson-Parent 2.5.0 和 3.0.0 发布
  6. Objective-C学习笔记-第一天(3)
  7. 搭建Tomcat6源代码阅读环境
  8. iOS view 颜色渐变
  9. 球形环境映射之angular方式的两种形式
  10. 【LeetCode OJ】Distinct Subsequences