编程之美2015 资格赛 hihocoder 题目2: 回文字符序列
2015-04-17 21:06
447 查看
思路:暴力搜,用BFS的方式,生成每一种可能,再对每一种可能进行判断是否回文,进行统计。严重超时!计算一个25个字符的,大概要20多秒!
AC了
#include <iostream> #include <string> #include <stdio.h> #include <memory> #include <string.h> using namespace std; long long map[1001][1001]; int pos[26]; int tmp[130]; int len; string input; void cal(int start ,int end) { if(start==end) //自己到自己,即单个字符的情况,是回文,也就是它自己 { map[start][end]=1; return ; } if(end-start==1) //两个字符的情况,判断两者是否相同。若相同,则有3个,否则,只有两个 { if(input[start]==input[end]) map[start][end]=3; else map[start][end]=2; return ; } map[start][end]+=map[start][end-1]+1; //先加一个,也就是他自己单独作为回文串。 /* for(int i=end-1; i>=start; i--) { if(input[i]==input[end]) //考虑end之前的每一个,对于第i个,如果和end相同,那么结果就是i到end之间的串的回文串的最大数量+1 map[start][end] += map[i+1][end-1]+1; //状态方程 } */ int temp = pos[end]; while( temp>-1 && temp>=start ) { map[start][end] += map[temp+1][end-1]+1; //状态方程 temp = pos[temp]; } map[start][end]%=100007; } void sao() { for(int i=0; i<26; i++) pos[i] = -1; // -1说明第1个字符的前面并没有与其相同的字符。 for(int i=0; i<130; i++) //记录最近一个字符出现的位置 tmp[i] = -1; for(int i=0; i<len; i++) { pos[i] = tmp[input[i]]; tmp[input[i]] = i; //更新字符input[i]最新出现的位置 } } int main() { //freopen("input.txt","r",stdin); int T; int Case=0; cin>>T; while(T--) { memset(map,0,sizeof(map)); cin>>input; len = input.size(); sao(); for(int i=0; i<input.size(); i++) //从左往右扫 { for(int j=i; j>=0; j--) //从第i个开始往左扫,才能保证在计算大问题之前,所要依靠的小问题已经被解决 { cal(j,i); } } cout<<"Case #"<<++Case<<": "<<map[0][input.size()-1]<<endl; } }
AC了
相关文章推荐
- 编程之美2015资格赛 题目2 : 回文字符序列
- 2015编程之美资格赛题目2 : 回文字符序列
- hiho 编程之美2015资格赛(回文字符序列-回文子序列数)
- 编程之美2015资格赛 B.回文字符序列
- 回文字符序列——微软编程之美2015资格赛第2题题解
- 回文字符序列(编程之美2015资格赛)
- 编程之美2015资格赛 题目2 : 回文字符序列 [ 区间dp ]
- 2015编程之美资格赛 B 回文字符序列
- 编程之美2015资格赛 B题 回文字符序列 (动态规划)
- 编程之美资格赛——题目2 : 回文字符序列
- 编程之美——题目2 : 回文字符序列
- 2015编程之美资格赛题目2 : 回文字符序列 分类: 算法 2015-04-21 11:40 61人阅读 评论(0) 收藏
- 编程之美资格赛 第二题 回文字符序列 dp
- 2015编程之美 回文字符序列(区间Dp)
- 2015编程之美资格赛:回文子序列数 DP
- 编程之美2015资格赛-题目1 2月29日
- 编程之美2015资格赛 题目3 : 基站选址
- 【编程之美】题目2 : 回文字符序列
- 【简单数学&DP】闰年计数&回文串计数 _Hihocoder战场 @ 编程之美2015资格赛
- 【hihoCoder】回文字符序列