UVa 10617 - Again Palindrome 字符串dp
2013-05-02 20:19
357 查看
ProblemI
AgainPalindromes
Input:StandardInput
Output:StandardOutputTimeLimit:2Seconds
Apalindormeisasequenceofoneormorecharactersthatreadsthesamefromtheleftasitdoesfromtheright.Forexample,Z,TOTandMADAMarepalindromes,butADAMisnot.
GivenasequenceSofNcapitallatinletters.Howmanywayscanonescoreoutafewsymbols(maybe0)thattherestofsequencebecomeapalidrome.Varintsthatareonlydifferentbyanorderofscoringoutshouldbeconsidered
thesame.
Input
Theinputfilecontainsseveraltestcases(lessthan15).ThefirstlinecontainsanintegerTthatindicateshowmanytestcasesaretofollow.EachoftheTlinescontainsasequenceS(1≤N≤60).Soactuallyeachoftheselinesisatestcase.
Output
Foreachtestcaseoutputinasinglelineaninteger–thenumberofways.SampleInputOutputforSampleInput
3 BAOBAB AAAA ABA | 22 15 5 |
----------------------
忽然发现我的字符串dp弱成渣啊。。。
f[i][j]表示从i到j的回文串个数。
当i!=j时,f[i][j]=f[i+1][j]+f[i][j-1]-f[i+1][j-1];从i到j的回文数=删除左边的字符后的回文数+删除右边字符后的回文数-重复部分
当i==j时,f[i][j]=f[i+1][j]+f[i][j-1]-f[i+1][j-1]+f[i+1][j-1]+1;从i到j的回文数=删除左边的字符后的回文数+删除右边字符后的回文数-重复部分+保留ij后的回文数+只留下ij的回文数即1
--------------------------
#include<cstdio>
#include<cstring>
#include<algorithm>
usingnamespacestd;
intT;
chars[111];
longlongf[111][111];
intn;
longlongdfs(intl,intr)
{
if(f[l][r]!=-1)returnf[l][r];
if(r-l<0)return0;
if(s[l]==s[r])
{
f[l][r]=dfs(l+1,r)+dfs(l,r-1)+1;
}
else
{
f[l][r]=dfs(l+1,r)+dfs(l,r-1)-dfs(l+1,r-1);
}
returnf[l][r];
}
intmain()
{
cin>>T;
while(T--)
{
memset(f,-1,sizeof(f));
cin>>(s+1);
n=strlen(s+1);
longlongans=dfs(1,n);
cout<<ans<<endl;
}
return0;
}
相关文章推荐
- UVa 10617 - Again Palindrome 字符串dp
- UVa 10617 Again Palindrome(回文串区间DP)
- UVA 字符串区间DP
- UVALive 6257 Chemist's vows 判断一个字符串是否由n中的某些字符串组成 dp
- UVa 10617 Again Palindrome(经典回文串区间DP)
- 【UVA 11584】【简单dp】Partitioning by Palindromes【给定一个字符串, 问最少分为几部分可使各部分均为回文串】
- Uva 10617 Again Palindrome(区间dp)
- UVa 11151 - Longest Palindrome 字符串dp
- uva10739(DP 回文字符串)
- UVA 10617 dp 题库210页
- UVa 10617 Again Palindrome(回文 区间dp)
- UVA11548 DP计算添加多少元素可以构成回文字符串
- uva10617 - Again Palindrome(dp)
- UVa 11151 - Longest Palindrome 字符串dp
- uva 10617 Again Palindrome (DP)
- UVA 10617 Again Palindrome 又是回文 dp,记忆化搜索
- UVA - 10617 Again Palindrome——dp
- UVA - 10617 Again Palindrome 字符串的回文子串
- uva 1351 String Compression(字符串区间dp)
- UVA 10617 Again Palindromes (区间dp)