TC SRM 607 DIV2
求拼接完成后的字符串包含的子回文串的数目,一开始还用暴力去做,想都不用想 肯定超时了。
复习了一下求最长子回文串的算法,发现可以类似解决。
给相邻字符之间添加一个‘@’字符,这样所有的回文串都是奇数长度,然后从左到右,统计以每个字符为中心的回文串的数目就行了,长度逐渐递增,一旦遇到不是回文串的情况,就马上结束,开始对下一个字符为中心的回文串的统计。
Orz…这个回文串算法不久前学过,最近搞前面基础去了就忘得差不多了,还是太弱了,继续好好努力,先打好基础,同时学点新东西,我就不相信,acm这东西花时间是搞不好的。
代码:
int count(vector <string> S1, vector <string> S2){
string tempstr = "", str = "";
int n1= S1.size(), n2 = S2.size();
REP(i, n1)
tempstr+= S1[i];
REP(i, n2)
tempstr+= S2[i];
int ans = 0;
int len = tempstr.size();
str += '@';
REP(i, len)
{
str += tempstr[i];
str += '@';
}
cout<<str<<' '<<str.size()<<endl;
len = 2*len+1;
REP(i, len) REP(k, inf){
if(!(i + k < len && i - k >= 0))
break;
if(str[i+k] == str[i-k])
ans += (str[i+k]!='@');
else break;
}
return ans;
}
TC SRM 607 DIV2的更多相关文章
- TC SRM 663 div2 B AABB 逆推
AABB Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description One day, Jamie noticed that many En ...
- TC SRM 663 div2 A ChessFloor 暴力
ChessFloor Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description Samantha is renovating a squa ...
- TC SRM 665 DIV2 A LuckyXor 暴力
LuckyXorTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description A lucky number is a positive int ...
- TC SRM 593 DIV2 1000
很棒的DP,不过没想出,看题解了..思维很重要. #include <iostream> #include <cstdio> #include <cstring> ...
- TC SRM 591 DIV2 1000
很不错的一题,非常巧妙的用DP顺序解决这个问题... 可以发现,只和A里面最小的有关系... #include <cstdio> #include <cstring> #inc ...
- tc srm 636 div2 500
100的数据直接暴力就行,想多了... ac的代码: #include <iostream> #include <cstdio> #include <cstring> ...
- TC SRM 664 div2 B BearPlaysDiv2 bfs
BearPlaysDiv2 Problem Statement Limak is a little bear who loves to play. Today he is playing by ...
- TC SRM 664 div2 A BearCheats 暴力
BearCheats Problem Statement Limak is an old brown bear. Because of his bad eyesight he sometime ...
- TC SRM 665 DIV2 B LuckyCycle 暴力
LuckyCycleTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...
随机推荐
- ASP过滤HTML标签
<% Function RemoveHTML(strHTML) Dim objRegExp, Match, Matches Set objRegExp = New Regexp objRegEx ...
- Java——String.split()函数
在java doc里有 String[] java.lang.String.split(String regex) Splits this string around matches of the g ...
- Dubbo在Spring和Spring Boot中的使用
一.在Spring中使用Dubbo 1.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifa ...
- myeclipse 右键 Add Struts... 页面报404 错误
网上试了很多种方法都不对,结果老师两下点出来了 我的改正方法是: 将WebRoot/WEB-INF/web.xml中的 <url-pattern>/*</url-pattern> ...
- Demo02_对结构体进行文件读写_张仕传_作业_
#include <iostream> using namespace std; #define StructArrarySize 5 // 老师数量 #define StudentNum ...
- rest和soap_笔记
Web 服务编程,REST 与 SOAP http://www.ibm.com/developerworks/cn/webservices/0907_rest_soap/ Web 服务编程,REST ...
- jQuery入门[1]-构造函数【转载】
最近看了一些jquery的进阶教程,感觉很不错,与大家分享下! jQuery——构造函数 ◦体积小(v1.2.3 15kb)◦丰富的DOM选择器(CSS1-3 + XPath) ◦跨浏览器(IE6,F ...
- html分页
<div class="fy"> <a href="" title="上一页">上一页</a> < ...
- 利用Keepalived+mysql构建高可用MySQL双主自动切转
转载:http://www.it300.com/index.php/article-15266.html 关于MySQL-HA,目前有多种解决方案,比如heartbeat.drbd.mmm.共享存储, ...
- PHP常见算法-面试篇(1)
1.冒泡排序 思路分析:在要排序的一组数中,对当前还未排好的序列,从前往后对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒.即,每当两相邻的数比较后发现它们的排序与排序要求相反时,就将 ...