http://community.topcoder.com/stat?c=problem_statement&pm=12967

计算一个字符串里Palindrome的数量。我的DP方法需要n^2的空间。

#include <vector>
#include <string>
using namespace std; class PalindromicSubstringsDiv2 {
public:
int count(vector <string> S1, vector <string> S2) {
string s;
for (int i = 0; i < S1.size(); i++) {
s += S1[i];
}
for (int i = 0; i < S2.size(); i++) {
s += S2[i];
}
int ret = 0;
vector<vector<bool>> dp;
int N = s.size();
dp.resize(N);
for (int i = 0; i < N; i++) {
dp[i].resize(N + 1);
}
for (int len = 1; len <= N; len++) {
for (int i = 0; i + len <= N; i++) { // start pos
if (len == 1) {
dp[i][len] = true;
} else if (len == 2){
dp[i][len] = (s[i] == s[i + len - 1]);
} else {
dp[i][len] = dp[i + 1][len - 2] && (s[i] == s[i + len - 1]);
}
ret += (dp[i][len] ? 1 : 0);
}
}
return ret;
}
};

如果从中间开始往两边扩,就不需要额外空间了~

#include <vector>
#include <string>
using namespace std; class PalindromicSubstringsDiv2 {
public:
int count(vector <string> S1, vector <string> S2) {
string s;
for (int i = 0; i < S1.size(); i++) {
s += S1[i];
}
for (int i = 0; i < S2.size(); i++) {
s += S2[i];
}
int count = 0;
int N = s.size();
for (int m = 0; m < N; m++) {
for (int even = 0; even < 2; even++) {
int i, j = 0;
if (even == 0) {
i = j = m;
} else {
i = m;
j = m + 1;
}
for (; i >= 0 && j < N; i--,j++) {
if (s[i] == s[j])
count++;
else
break;
}
}
}
return count;
}
};

  

*[topcoder]PalindromicSubstringsDiv2的更多相关文章

  1. TopCoder kawigiEdit插件配置

    kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...

  2. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  3. TopCoder比赛总结表

    TopCoder                        250                              500                                 ...

  4. Topcoder几例C++字符串应用

    本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...

  5. TopCoder

    在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...

  6. TopCoder SRM 596 DIV 1 250

    body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...

  7. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

  8. TopCoder SRM 590

     第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement     Fox Ciel is going to play Gomoku with her friend ...

  9. Topcoder Arena插件配置和训练指南

    一. Arena插件配置 1. 下载Arena 指针:http://community.topcoder.com/tc?module=MyHome 左边Competitions->Algorit ...

随机推荐

  1. WinDbg 调试.net程序

    WinDbg支持以下三种类型的命令: ·        常规命令,用来调试进程 ·        点命令,用来控制调试器 ·        扩展命令,可以添加叫WinDbg的自定义命令,一般由扩展dl ...

  2. CLR via C# 序列化读书笔记

    1. 序列化格式类 a. 二进制BinaryFormatter b. XML流 NetDataContractSerializer c. CLR类据类型与非CLR数据类型之间互操作 XmlSerial ...

  3. 感知器算法PLA

    for batch&supervised binary classfication,g≈f <=> Eout(g)≥0 achieved through Eout(g)≈Ein(g ...

  4. 转发 python中file和open有什么区别

    python中file和open有什么区别?2008-04-15 11:30地痞小流氓 | 分类:python | 浏览3426次python中file和open有什么区别?都是打开文件,说的越详细越 ...

  5. SQL效率的几点心得

    这几天一直在写SQL,有时候对比同样效果的SQL语句,可是查询所需要的时间有时候相差很多,下面总结遇到的几个点: 1.between   and 在有些时候自己比较喜欢使用这个语句,因为可以通过把数据 ...

  6. CentOS 6.2编译安装Nginx1.2.0+MySQL5.5.25+PHP5.3.13+博客系统WordPress3.3.2

    说明: 操作系统:CentOS 6.2 32位 系统安装教程:CentOS 6.2安装(超级详细图解教程): http://www.osyunwei.com/archives/1537.html 准备 ...

  7. advance 模板 怎么生成module

    advance 模板 怎么生成module namespace写什么如果是前台呢就是 frontend\modules\modulename\Module@我叫红领巾 module id有什么用bak ...

  8. Qt版helloworld

    跟学别的编程语言一样,Qt也不例外,一开始就想写一个helloworld.初学Qt十几天,看了一点关于Qt视频的介绍和书上的基础知识,对于Qt写工程的概念有了初步的认识,就代码的形式来说,Qt Cre ...

  9. iTween基础之Rotate(旋转角度)

    一.基础介绍:二.基础属性 原文地址 :http://blog.csdn.net/dingkun520wy/article/details/50696489 一.基础介绍 RotateTo:旋转游戏物 ...

  10. C#调用PowerShell的经历

    好久没有写程序了, 再次上手也处于功能强大的Windows PowerShell的缘故. 不多话, 先上段代码引入正题.... static Collection<PSObject> Ru ...