hard or 9102 字符串DP---Educational Codeforces Round 57 (Rated for Div. 2)

题意:http://codeforces.com/problemset/problem/1096/D

思路:参考:https://blog.csdn.net/qq_41289920/article/details/100715683

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\Users\13606\Desktop\草稿.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 //******************
 24 int abss(int a);
 25 int lowbit(int n);
 26 int Del_bit_1(int n);
 27 int maxx(int a,int b);
 28 int minn(int a,int b);
 29 double fabss(double a);
 30 void swapp(int &a,int &b);
 31 clock_t __STRAT,__END;
 32 double __TOTALTIME;
 33 void _MS(){__STRAT=clock();}
 34 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 35 //***********************
 36 #define rint register int
 37 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 38 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 39 #define mem(a,b) memset(a,b,sizeof(a))
 40 #define pr printf
 41 #define sc scanf
 42 #define ls rt<<1
 43 #define rs rt<<1|1
 44 typedef long long ll;
 45 const double E=2.718281828;
 46 const double PI=acos(-1.0);
 47 //const ll INF=(1LL<<60);
 48 const int inf=(1<<30);
 49 const double ESP=1e-9;
 50 const int mod=(int)1e9+7;
 51 const int N=(int)1e6+10;
 52 
 53 char s[N];
 54 ll dp[N][5];
 55 ll a[N];
 56 
 57 int main()
 58 {
 59     int n;
 60     sc("%d",&n);
 61     sc("%s",s+1);
 62     for(int i=1;i<=n;++i)sc("%lld",&a[i]);
 63     for(int i=1;i<=n;++i)
 64     {
 65         for(int j=1;j<=4;++j)dp[i][j]=dp[i-1][j];
 66         if(s[i]=='h')dp[i][1]+=a[i];
 67         else if(s[i]=='a')dp[i][2]=min(dp[i][2]+a[i],dp[i-1][1]);
 68         else if(s[i]=='r')dp[i][3]=min(dp[i][3]+a[i],dp[i-1][2]);
 69         else if(s[i]=='d')dp[i][4]=min(dp[i][4]+a[i],dp[i-1][3]);
 70     }
 71     pr("%lld
",dp[n][4]);
 72     return 0;
 73 }
 74 
 75 /**************************************************************************************/
 76 
 77 int maxx(int a,int b)
 78 {
 79     return a>b?a:b;
 80 }
 81 
 82 void swapp(int &a,int &b)
 83 {
 84     a^=b^=a^=b;
 85 }
 86 
 87 int lowbit(int n)
 88 {
 89     return n&(-n);
 90 }
 91 
 92 int Del_bit_1(int n)
 93 {
 94     return n&(n-1);
 95 }
 96 
 97 int abss(int a)
 98 {
 99     return a>0?a:-a;
100 }
101 
102 double fabss(double a)
103 {
104     return a>0?a:-a;
105 }
106 
107 int minn(int a,int b)
108 {
109     return a<b?a:b;
110 }
原文地址:https://www.cnblogs.com/--HPY-7m/p/11735238.html