CodeForces 159D 字符串处理
2013-07-31 19:30
330 查看
本题题意先是没搞清楚,后来问了一下才知道。是给你一组字符串让你找有多少字符串对。例如:aaa中(简称a1,a2,a3)a1是一个字符串,能和a2组成一对,a3组成一对,能和a2a3组成一对,a2和a3,a1a2和a3,一共5对。
而解决思路是利用两个数组,一个正向记有多少回文串数,一个反向记有多少回文串数,最后一步一步走,例如:abacaba,一步一步走表示为[a][bacaba];前面的回文串数乘以后面的回文串数;然后
[ab][acaba],最后相加起来就是最终对数。
代码:
而解决思路是利用两个数组,一个正向记有多少回文串数,一个反向记有多少回文串数,最后一步一步走,例如:abacaba,一步一步走表示为[a][bacaba];前面的回文串数乘以后面的回文串数;然后
[ab][acaba],最后相加起来就是最终对数。
代码:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=2500; char f[maxn]; int len; __int64 g[maxn],h[maxn]; bool is_palind(int i,int j) { while(i<j) { if(f[i]!=f[j])return false; i++,j--; } return true; } void Init() { for(int i=1; i<=len; i++) { g[i]=0; for(int j=1; j<=i; j++) if(is_palind(j,i)) g[i]++;//g[i]表示以第i个字母为终点, //以i之前的字母为起点的所有回文串的数量 } for(int i=len; i>=1; i--) { h[i]=0; for(int j=len; j>=i; j--) if(is_palind(i,j)) h[i]++;//h[i]表示以第i个字母为起点, //以比i 大的字母为终点的回文串的数量 } } int main() { while(scanf("%s",f+1)!=EOF) { len=strlen(f+1); Init(); for(int i=1; i<=len; ++i) //cout<<g[i]<<' '; cout<<endl; //for(int j=1;j<=len;++j) // cout<<h[j]<<' '; cout<<endl; __int64 ans=0; for(int i=1; i<len; i++) for(int j=i+1; j<=len; j++) ans+=g[i]*h[j]; printf("%I64d\n",ans); } return 0; }
相关文章推荐
- Codeforces 1B (字符串的26进制处理)
- [codeforces]A. Efim and Strange Grade——贪心+字符串处理
- Codeforces 448B (字符串简单处理,读题)
- 【CodeForces 159C】String Manipulation 1.0(字符串处理)
- codeforces 200D Programming Language 字符串处理
- codeforces 883E. Field of Wonders (字符串处理)
- CodeForces 4C Registration system(字符串处理 | map)
- codeforces 190C STL 字符串处理+栈思想
- 【CodeForces 777D】Cloud of Hashtags (模拟+字符串处理)
- Codeforces 727B 字符串处理
- codeforces 883F. Lost in Transliteration (字符串处理)
- codeforces_600A. Extract Numbers(字符串处理)
- CodeForces - 827A String Reconstruction(思维+字符串处理)
- codeforces - 738A - Interview with Oleg(字符串处理)
- CodeForces 23A You're Given a String...(字符串处理)
- codeforces 195C Try and Catch 字符串处理
- 【CodeForces 155C Hometask】白濑肆×字符串+DP——果然是字符串处理什么的好讨厌啊尤其是换行符的处理看来不用CIN不行了呢DP的转移真心不会啊水到家了怎么办!【1.1%达成】
- Codeforces 723B Text Document Analysis (字符串处理)
- 2018.2.2【 CodeForces - 501B 】解题报告(STL,map,字符串处理)
- CodeForces--TechnoCup--2016.10.15--ProblemB--Bill Total Value(字符串处理)