Again Palindromes - UVa 10617 dp
2014-07-31 15:11
561 查看
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 |
思路:还是从边上往中间递推if(s[l]==s[r]) dp[l][r]=dp[l+1][r]+dp[l][r-1]+1; elsedp[l][r]=dp[l+1][r]+dp[l][r-1]-dp[l+1][r-1];
AC代码如下:
#include<cstring>
usingnamespacestd;
longlongdp[100][100],;
chars[110];
voiddfs(intl,intr)
{if(dp[l][r]>=0)
return;
if(l>r)
{dp[l][r]=0;
return;
}
dfs(l+1,r);
dfs(l,r-1);
dfs(l+1,r-1);
if(s[l]==s[r])
dp[l][r]=dp[l+1][r]+dp[l][r-1]+1;
else
dp[l][r]=dp[l+1][r]+dp[l][r-1]-dp[l+1][r-1];
}
intmain()
{intt,n,m,i,j,k,len;
scanf("%d",&t);
while(t--)
{scanf("%s",s+1);
len=strlen(s+1);
memset(dp,-1,sizeof(dp));
for(i=1;i<=len;i++)
dp[i][i]=1;
dfs(1,len);
printf("%lld\n",dp[1][len]);
}
}
相关文章推荐
- uva 10617(dp)
- UVA 10617 区间DP
- UVa 10617 - Again Palindrome 字符串dp
- UVA 10617 Again Palindrome 又是回文 dp,记忆化搜索
- UVA 题目10617 - Again Palindrome(区间DP)
- uva10617 - Again Palindrome(dp)
- UVa 10617 - Again Palindrome 字符串dp
- UVa 10617 Again Palindrome(回文 区间dp)
- UVA 10617 dp 题库210页
- uva 10617 Again Palindromes(DP)
- UVA - 10617 Again Palindrome——dp
- Uva 10617 Again Palindrome(区间dp)
- uva 10617 Again Palindrome (DP)
- UVA 10617 Again Palindromes (区间dp)
- UVa 10617 Again Palindrome(回文串区间DP)
- UVa 10617 Again Palindrome(经典回文串区间DP)
- uva10617 - Again Palindrome(dp)
- uva_10617_Again Palindrome(普通DP, 记忆化搜索)
- uva 10617 - Again Palindrome(dp)
- UVa在线比赛单题汇总-----DP专题