Codeforces 706C Hard problem (DP)
来源:互联网 发布:淘宝链接付款安全吗 编辑:程序博客网 时间:2024/06/16 18:22
#include<math.h>#include<string>#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespace std;long long a[100005];string str1[100005],str2[100005];long long dp[100005][2];int main(){ int t; scanf("%d",&t); for(int i=1;i<=t;i++) { scanf("%I64d",&a[i]); } for(int i=1;i<=t;i++) { cin>>str1[i]; str2[i]=string(str1[i].rbegin(),str1[i].rend()); } for(int i=1;i<=t;++i) dp[i][0]=dp[i][1]=1e15; dp[1][0]=0; dp[1][1]=a[1]; int i; for(i=2;i<=t;i++) { if(str1[i-1]<=str1[i]) dp[i][0]=dp[i-1][0]; if(str1[i-1]<=str2[i]) dp[i][1]=dp[i-1][0]+a[i]; if(str2[i-1]<=str1[i]) dp[i][0]=min(dp[i-1][1],dp[i][0]); if(str2[i-1]<=str2[i]) dp[i][1]=min(dp[i-1][1]+a[i],dp[i][1]); if(dp[i][0]==1e15&&dp[i][1]==1e15) break; //printf("%d %d\n",dp[i][0],dp[i][1]); } if(i==t+1) printf("%I64d\n",min(dp[t][1],dp[t][0])); else printf("-1\n");}
题意:
给定一个序列C 表示 翻转 字符串 的花费。
求使 从上到下 可以翻转 字符串 字典序不降。
最终结果可以为:
ab (ab)<(ac)
ac
aa (aa)<(ab)<(ac)
ab
ac
bbb 不管怎么翻转 都不会使 字典序 不降。
aaa
对于字符串一定存在两种状态。翻转或者不翻转。dp【i】【0】表示不翻转 dp【i】【1】表示翻转。
i-1不翻 i翻 str1【i-1】<str2【i】 (考虑需不需要加上条件 str1【i-1】>str1【i】)不需要。。。。。加上条件是 具体的情况下进行转换。
这个条件存在是因为当 str1【i-1】<str2【i】成立,是因为str1【i-1】>str1【i】成立 不加条件是 状态进行转换。
i-1翻 i不翻 str【i-1】<str1【i】
i-1翻 i不翻 str2【i-1】<str2【i】
i-1不翻 i不翻 str1【i-1】<str1【i】
断网啦。。。
0 0
- Codeforces 706C Hard problem(dp+字符串)
- Codeforces 706C Hard problem (DP)
- Codeforces 706C Hard problem (DP)
- CodeForces 706C Hard problem【dp】
- CodeForces 706 C. Hard problem(dp)
- CodeForces 706C-Hard problem(基本的DP)
- CodeForces 367C Hard problem DP
- Codeforces-706C Hard problem
- CodeForces 706C Hard problem
- 【Codeforces 706C . Hard problem】
- codeforces 706C Hard problem
- Codeforces 706C Hard problem
- codeforces 706 C. Hard problem #367 div2
- 【动态规划】Codeforces 706C Hard problem
- Codeforces Round #367 (Div. 2)-C - Hard problem-DP
- 【Codeforces Round 367 (Div 2) C】【简单DP】Hard problem
- Codeforces Round #367 (Div. 2) C. Hard problem (DP)
- Codeforces Round #367 (Div. 2) C. Hard problem (dp)
- maven项目报Invalid bound statement (not found): com.**.*解决方案
- 2015.7-2-16.8__期间工作的一个小结
- FileInfo.Delete()时,没有权限,提示“无法删除,拒绝访问...”
- c++之指针作为函数参数传递的问题
- NO.3 Python爬虫
- Codeforces 706C Hard problem (DP)
- U3D Scripting API GL
- Python读取多个txt文件并进行保存
- React Native通信机制详解
- mysql windows下搭建mysql主从+注意事项
- Codeforces Round #369 (Div. 2) 711C Coloring Tree (DP)
- Unity3D 使用 GL 绘制线条
- Java 基础部分
- NYOJ 116 士兵杀敌(二)