CodeForces 132C Logo Turtle :一个字符串包含T和F,准确改变n个字符使走的距离最长:记忆化搜索

范围比较小,直接记忆化搜索=

dp[go][pos][i][cnt]表示朝向,位置,字符串行进位置,剩余操作数

转移方程思考=

注意cnt先可以-2

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 int dp[2][205][105][55],len;
 6 char s[105];
 7 int dfs(int go,int pos,int i,int cnt)
 8 {
 9   if (cnt<0) return 0;
10   if (i>=len) return cnt>0?0:abs(pos);
11   if (dp[go][pos+100][i][cnt]!=-1) return dp[go][pos+100][i][cnt];
12   dp[go][pos+100][i][cnt]=max(dfs(!go,pos,i+1,cnt-(s[i]!='T')),
13 dfs(go,pos+(go?1:-1),i+1,cnt-(s[i]!='F')));
14   return dp[go][pos+100][i][cnt];
15 }
16 int main()
17 {
18   int ans,cnt;
19   scanf("%s%d",s,&cnt);
20   memset(dp,-1,sizeof(dp));
21   ans=0; len=strlen(s);
22   while (cnt>=0){
23     ans=max(ans,dfs(1,0,0,cnt));
24     cnt-=2;
25   }
26   printf("%d\n",ans);
27   return 0;
28 }
View Code

题目链接:http://codeforces.com/problemset/problem/132/C

posted on 2015-03-25 22:13  xiao_xin  阅读(150)  评论(0编辑  收藏  举报

导航