*[topcoder]PalindromicSubstringsDiv2
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的更多相关文章
- TopCoder kawigiEdit插件配置
kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- TopCoder比赛总结表
TopCoder 250 500 ...
- Topcoder几例C++字符串应用
本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...
- TopCoder
在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...
- TopCoder SRM 596 DIV 1 250
body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- TopCoder SRM 590
第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement Fox Ciel is going to play Gomoku with her friend ...
- Topcoder Arena插件配置和训练指南
一. Arena插件配置 1. 下载Arena 指针:http://community.topcoder.com/tc?module=MyHome 左边Competitions->Algorit ...
随机推荐
- PHP利用socket_bind函数切换IP地址采集数据
在利用PHP进行数据采集的过程中,通常会遇到IP被屏蔽或出现验证码的情况:为了能够继续采集,我们需要切换不同的ip,每访问一次,随机切换一个IP.当然也可以通过收集大量代理,通过切换代理的方式进行采集 ...
- CodeIgniter(CI 3.0)分页类实践记录
最近在学习B/S,选择了PHP CI框架作为切入点. 在尝试制作个人CMS的时候遇到了需要分页的情况,网上好像搜不到3.0版本以上的例子,下面附上本地实验的代码,供参考. 数据库情况如下: 首先看Co ...
- iblog语法高亮示例
-------------------------------------------------------------------------------------- iblog 是一款 Sub ...
- mvvm结构中数据的关联----wpf
1.在视图中PlotView.xaml <Button Content="<<" Height="23" HorizontalAlignmen ...
- boost-内存管理
Boost智能指针——scoped_ptr boost::scoped_ptr和std::auto_ptr非常类似,是一个简单的智能指针,它能够保证在离开作用域后对象被自动释放. boost::sco ...
- UIActivityIndicatorView的使用
class ViewController: UIViewController,UIActionSheetDelegate{ @IBOutlet weak var label1: UILabel! @I ...
- Legacy安装win7和Ubuntu14.04双系统
Legacy安装win7和Ubuntu14.04双系统 安装环境 Legacy启动模式(传统引导) 笔记本已安装win7 硬盘启动顺序为: U盘 硬盘 光驱 安装方法 制作U盘启动盘 在Ubuntu官 ...
- ubuntu 14.04 安装 Quartus II 13.1 过程
神奇的linux! 第一步去官网注册然后下载对应的linux版本,包括软件和设备文件两部分,软件也就是quartus II nios ide,modelsim-altera这些,设备就是具体alter ...
- Java 8 VM GC Tunning Guide Charter 6
第六章 并行GC The Parallel Collector The parallel collector (also referred to here as the throughput coll ...
- android开发设置dialog的高宽
这里设置为跟屏幕一样的宽度,:看代码 dlg.show(); WindowManager.LayoutParams params = dlg.getWindow().getAttributes(); ...