当前位置:网站首页>Invoker 2019CCPC秦皇岛站I题 简单DP

Invoker 2019CCPC秦皇岛站I题 简单DP

2022-08-09 07:01:00 swust_fang

题目链接

每个技能有6中组合,上一个技能也有6种组合,所以从该状态6种分别从上种的6种转移过来取最小值即可。

不读题的话可能就看成两种状态了(hh

ss表示当前状态,s[k]表示上个状态,check函数表示状态转移所需

dp[i][j]=min(dp[i-1][k]+check(s[k],ss),dp[i][j])

代码:

//#pragma comment (linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<list>
#include<time.h>
#define myself i,l,r
#define lson i<<1
#define rson i<<1|1
#define Lson i<<1,l,mid
#define Rson i<<1|1,mid+1,r
#define half (l+r)/2
#define lowbit(x) x&(-x)
#define min4(a, b, c, d) min(min(a,b),min(c,d))
#define min3(x, y, z) min(min(x,y),z)
#define max3(x, y, z) max(max(x,y),z)
#define max4(a, b, c, d) max(max(a,b),max(c,d))
typedef unsigned long long ull;
typedef long long ll;
#define pii make_pair
#define pr pair<int,int>
const int inff = 0x3f3f3f3f;
const long long inFF = 9223372036854775807;
const int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
const int mdir[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 1, -1, -1, -1};
const double eps = 1e-10;
const double PI = acos(-1.0);
const double E = 2.718281828459;
using namespace std;
const int mod=998244353;
const int maxn=3e5+5;
const int maxm=1e6+5;
map<char,int> m;
char str[maxn];
ll dp[maxn][6];
int check(string s,string s1)
{
    if(s==s1) return 1;
    else if(s[1]==s1[0]&&s[2]==s1[1]) return 2;
    else if(s[2]==s1[0]) return 3;
    return 4;
}
int main()
{
    string sc[10][6]={
            {"QQQ","QQQ","QQQ","QQQ","QQQ","QQQ"},
            {"QQW","QWQ","WQQ","WQQ","WQQ","WQQ"},
            {"QQE","QEQ","EQQ","EQQ","EQQ","EQQ"},
            {"WWW","WWW","WWW","WWW","WWW","WWW"},
            {"QWW","WQW","WWQ","WWQ","WWQ","WWQ"},
            {"WWE","WEW","EWW","EWW","EWW","EWW"},
            {"EEE","EEE","EEE","EEE","EEE","EEE"},
            {"QEE","EQE","EEQ","EEQ","EEQ","EEQ"},
            {"WEE","EWE","EEW","EEW","EEW","EEW"},
            {"QWE","QEW","EQW","EWQ","WEQ","WQE"},
    };
    m['Y']=0; m['V']=1; m['G']=2; m['C']=3; m['X']=4;
    m['Z']=5; m['T']=6; m['F']=7; m['D']=8; m['B']=9;
    while(scanf("%s",str+1)!=EOF)
    {
        int len=strlen(str+1);
        string s[6];
        s[0]="   ",s[1]="   ",s[2]="   ",s[3]="   ",s[4]="   ",s[5]="   ";
        for(int i=1;i<=len;i++)
        {
            int x=m[str[i]];
            for(int j=0;j<6;j++)
            {
                string ss=sc[x][j];
                dp[i][j]=inff;
                for(int k=0;k<6;k++)
                    dp[i][j]=min(dp[i-1][k]+check(s[k],ss),dp[i][j]);
            }
            for(int j=0;j<6;j++) s[j]=sc[x][j];
        }
        ll ans=inff;
        for(int i=0;i<6;i++) ans=min(dp[len][i],ans);
        printf("%lld\n",ans);
    }
}

 

原网站

版权声明
本文为[swust_fang]所创,转载请带上原文链接,感谢
https://blog.csdn.net/swust5120171204/article/details/102477549