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

给你n个字符串,可以反转任意一个字符串,反转每个字符串都有其对应的花费ci。

经过操作后是否能满足字符串str[i]>=str[i-1],能就输出最小花费,不能输出-1。

dp[i][0] 表示不反转i的最小花费(str[i] >= str[i - 1] || str[i] >= reverse(str[i - 1]))

dp[i][1] 则表示反转i的最小花费...

初始dp[1][0] = 0, dp[1][1] = c[1]

要是dp[i][0/1]等于-1 就不能转移了

代码写的有点糟糕,还是太渣...

  1. //#pragma comment(linker, "/STACK:102400000, 102400000")
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cstdio>
  7. #include <string>
  8. #include <vector>
  9. #include <cmath>
  10. #include <ctime>
  11. #include <list>
  12. #include <set>
  13. #include <map>
  14. using namespace std;
  15. typedef long long LL;
  16. typedef pair <int, int> P;
  17. const int N = 1e5 + ;
  18. string str[N];
  19. LL num[N], inf = 1e15;
  20. LL dp[N][];
  21.  
  22. int main()
  23. {
  24. ios::sync_with_stdio(false);
  25. int n;
  26. cin >> n;
  27. for(int i = ; i <= n; ++i)
  28. cin >> num[i];
  29. for(int i = ; i <= n; ++i)
  30. cin >> str[i];
  31. int ok = ;
  32. memset(dp, -, sizeof(dp));
  33. dp[][] = , dp[][] = num[];
  34. for(int i = ; i <= n; ++i) {
  35. string str1 = str[i - ]; //未反转
  36. reverse(str[i - ].begin(), str[i - ].end());
  37. string str2 = str[i]; //未反转
  38. reverse(str[i].begin(), str[i].end());
  39. if(dp[i - ][] == - && dp[i - ][] == -) {
  40. ok = -; //不行了
  41. break;
  42. }
  43. if(dp[i - ][] != -) {
  44. if(str2 >= str1) {
  45. dp[i][] = dp[i - ][];
  46. }
  47. if(str[i] >= str1) {
  48. dp[i][] = dp[i - ][] + num[i];
  49. }
  50. }
  51. if(dp[i - ][] != -) {
  52. if(str2 >= str[i - ]) {
  53. dp[i][] = min(dp[i - ][], dp[i][] == - ? inf : dp[i][]);
  54. }
  55. if(str[i] >= str[i - ]) {
  56. dp[i][] = min(dp[i - ][] + num[i], dp[i][] == - ? inf : dp[i][]);
  57. }
  58. }
  59. str[i] = str2; //赋值未反转
  60. }
  61. if(ok == -) {
  62. cout << - << endl;
  63. }
  64. else if(dp[n][] != - && dp[n][] != -) {
  65. cout << min(dp[n][], dp[n][]) << endl;
  66. }
  67. else if(dp[n][] != -) {
  68. cout << dp[n][] << endl;
  69. }
  70. else if(dp[n][] != -) {
  71. cout << dp[n][] << endl;
  72. }
  73. else {
  74. cout << - << endl;
  75. }
  76. return ;
  77. }

Codeforces 706 C. Hard problem (dp)的更多相关文章

  1. codeforces 706C C. Hard problem(dp)

    题目链接: C. Hard problem time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)

    题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...

  3. codeforces 721C (拓排 + DP)

    题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...

  4. Codeforces 543D. Road Improvement (树dp + 乘法逆元)

    题目链接:http://codeforces.com/contest/543/problem/D 给你一棵树,初始所有的边都是坏的,要你修复若干边.指定一个root,所有的点到root最多只有一个坏边 ...

  5. Codeforces 467C. George and Job (dp)

    题目链接:http://codeforces.com/contest/467/problem/C 求k个不重叠长m的连续子序列的最大和. dp[i][j]表示第i个数的位置个序列的最大和. 前缀和一下 ...

  6. CodeForces 163A Substring and Subsequence dp

    A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...

  7. Codeforces 834D The Bakery【dp+线段树维护+lazy】

    D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...

  8. codeforces#1154F. Shovels Shop (dp)

    题目链接: http://codeforces.com/contest/1154/problem/F 题意: 有$n$个物品,$m$条优惠 每个优惠的格式是,买$x_i$个物品,最便宜的$y_i$个物 ...

  9. Educational Codeforces Round 63-D(基础DP)

    题目链接:https://codeforces.com/contest/1155/problem/D 题意:给定n个数,可以选择一段连续子段将其乘x,也可以不操作,求最大连续子段和. 思路:比赛时觉得 ...

随机推荐

  1. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  2. Kafka中的Message Delivary机制

    学习Kafka的读书笔记,暂未把文章设为翻译类型,因为并非直译文档.水平有限,还请路过高手指正. <1> “最多(发送)一次”(At most once):消息可以丢失但绝不会重新发送:& ...

  3. mysql 数据库常用命令总结

    (1)查看数据库可以支持的存储引擎  命令:show engines;  (2)查看表结构命令:desc table_name:(3)显示表的创建语句     show create table ta ...

  4. (六)6.9 Neurons Networks softmax regression

    SoftMax回归模型,是logistic回归在多分类问题的推广,即现在logistic回归数据中的标签y不止有0-1两个值,而是可以取k个值,softmax回归对诸如MNIST手写识别库等分类很有用 ...

  5. Heritrix源码分析(五) 如何让Heritrix在Ecplise等IDE下编程启动(转)

    本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/642550      本博客已迁移到本人独立博客: http://www.yun5u. ...

  6. Python 字典(Dictionary) get()方法

    描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...

  7. Android实例] android获取web服务器端session并验证登陆

    传统网页实现用户登陆一般采用session或cookie记录用户基本信息又或者两者结合起来使用.android也可以采用session实现用户登陆验证并记录用户登陆状态时的基本信息,session是在 ...

  8. Yii连接多个数据库的方法

    一.配置多数据库 大多数情况下,我们都会采用同一类型的数据库,只是为了缓解压力分成主从或分布式形式而已.声明你可以在 主配置文件 ( main.php )   中里声明其它的数据库连接: <?p ...

  9. Drupal如何实现类的自动加载?

    Drupal通过spl_autoload_register()注册类加载器实现自动加载: function _drupal_bootstrap_database() { // ... .... spl ...

  10. C#拼音转换,将简体中文转换成拼音

    1. 要进行拼音转换操作,首先要引入几个文件,也就是用于操作拼音转换的文件,就是微软提供给开发者的一个类库 Microsoft Visual Studio International Pack 1.0 ...