2019CCPC秦皇岛I题 Invoker(DP)
Invoker
Time Limit: 15000/12000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 117 Accepted Submission(s): 35
Problem Description
In dota2, there is a hero named Invoker. He has 3 basic skills in the game, which are Quas, Wex and Exort. Once he launches a basic skill, he will gain the corresponding element, where Quas gives "Q", Wex gives "W" and Exort gives "E".
Invoker can't have more than 3 elements simultaneously. If he launches a basic skill when he already owns 3 elements, he will get the corresponding element and lose the element he gained the earliest.
As can be seen, there are 10 unordered combinations of 3 elements in 3 types, each represents a special skill, which are as follows:
When Invoker owns 3 elements, he can launch the invoking skill, denoted by "R", to gain the special skill according to the elements he currently owns. After invoking, the elements won't disappear, and the chronological order of the 3 elements won't change.
Now given a sequence of special skills, you want to invoke them one by one with using the minimum number of basic skills(Q,W,E) and invoking skill(R). Print the minimum number in a single line.
At the beginning, Invoker owns no elements. And you should re-invoke the special skills even if you have already invoked the same skills just now.
Invoker can't have more than 3 elements simultaneously. If he launches a basic skill when he already owns 3 elements, he will get the corresponding element and lose the element he gained the earliest.
As can be seen, there are 10 unordered combinations of 3 elements in 3 types, each represents a special skill, which are as follows:
- Cold Snap: unordered element combination "QQQ", denoted by "Y"
- Ghost Walk: unordered element combination "QQW", denoted by "V"
- Ice Wall: unordered element combination "QQE", denoted by "G"
- EMP: unordered element combination "WWW", denoted by "C"
- Tornado: unordered element combination "QWW", denoted by "X"
- Alacrity: unordered element combination "WWE", denoted by "Z"
- Sun Strike: unordered element combination "EEE", denoted by "T"
- Forge Spirit: unordered element combination "QEE", denoted by "F"
- Chaos Meteor: unordered element combination "WEE", denoted by "D"
- Deafening Blast: unordered element combination "QWE", denoted by "B"
When Invoker owns 3 elements, he can launch the invoking skill, denoted by "R", to gain the special skill according to the elements he currently owns. After invoking, the elements won't disappear, and the chronological order of the 3 elements won't change.
Now given a sequence of special skills, you want to invoke them one by one with using the minimum number of basic skills(Q,W,E) and invoking skill(R). Print the minimum number in a single line.
At the beginning, Invoker owns no elements. And you should re-invoke the special skills even if you have already invoked the same skills just now.
Input
Input a single line containing a string s (1 ≤ |s| ≤ 100 000) that only contains uppercase letters in {B, C, D, F, G, T, V, X, Y, Z}, denoting the sequence of special skills.
Output
Output a single line containing a positive integer, denoting the minimum number of skills to launch.
Sample Input
XDTBVV
Sample Output
15
Hint
One possible scheme is QWWREERERWQRQRR.
Source
参考代码:

#include<bits/stdc++.h> using namespace std; const int size=1e5+5; const int inf=0x3f3f3f3f; char ss[size]; int dp[size][5][5]; int ans[size]; char s[15][5]={"QQQ","QQW","QQE","WWW","QWW","WWE","EEE","QEE","WEE","QWE"}; int stru[6][3]={{0,1,2},{0,2,1},{1,0,2},{1,2,0},{2,1,0},{2,0,1}}; inline int id(char c) { if(c=='Q') return 0; if(c=='W') return 1; if(c=='E') return 2; } int ty[size]; inline int swap(char c) { if(c=='Y') return 0; if(c=='V') return 1; if(c=='G') return 2; if(c=='C') return 3; if(c=='X') return 4; if(c=='Z') return 5; if(c=='T') return 6; if(c=='F') return 7; if(c=='D') return 8; if(c=='B') return 9; } int main() { while(~scanf("%s",ss+1)) { int len=strlen(ss+1); for(int i=0;i<=len;i++) { for(int j=0;j<=2;j++) { for(int k=0;k<=2;k++) { dp[i][j][k]=inf; } } } for(int i=1;i<=len;i++) { ty[i]=swap(ss[i]); ans[i]=inf; } ans[1]=3; dp[1][id(s[ty[1]][0])][id(s[ty[1]][1])]=3; dp[1][id(s[ty[1]][0])][id(s[ty[1]][2])]=3; dp[1][id(s[ty[1]][1])][id(s[ty[1]][0])]=3; dp[1][id(s[ty[1]][1])][id(s[ty[1]][2])]=3; dp[1][id(s[ty[1]][2])][id(s[ty[1]][1])]=3; dp[1][id(s[ty[1]][2])][id(s[ty[1]][0])]=3; for(int i=2;i<=len;i++) { if(ty[i]==ty[i-1]) { for(int j=0;j<=2;j++) { for(int k=0;k<=2;k++) { dp[i][j][k]=dp[i-1][j][k]; } } ans[i]=ans[i-1]; continue; } dp[i][id(s[ty[i]][0])][id(s[ty[i]][1])]=3+ans[i-1]; dp[i][id(s[ty[i]][0])][id(s[ty[i]][2])]=3+ans[i-1]; dp[i][id(s[ty[i]][1])][id(s[ty[i]][0])]=3+ans[i-1]; dp[i][id(s[ty[i]][1])][id(s[ty[i]][2])]=3+ans[i-1]; dp[i][id(s[ty[i]][2])][id(s[ty[i]][1])]=3+ans[i-1]; dp[i][id(s[ty[i]][2])][id(s[ty[i]][0])]=3+ans[i-1]; for(int j=0;j<6;j++) { dp[i][id(s[ty[i]][stru[j][1]])][id(s[ty[i]][stru[j][2]])]=min(dp[i][id(s[ty[i]][stru[j][1]])][id(s[ty[i]][stru[j][2]])],dp[i-1][id(s[ty[i]][stru[j][0]])][id(s[ty[i]][stru[j][1]])]+1); for(int k=0;k<=2;k++) dp[i][id(s[ty[i]][stru[j][1]])][id(s[ty[i]][stru[j][2]])]=min(dp[i][id(s[ty[i]][stru[j][1]])][id(s[ty[i]][stru[j][2]])],dp[i-1][k][id(s[ty[i]][stru[j][0]])]+2); ans[i]=min(ans[i],dp[i][id(s[ty[i]][stru[j][1]])][id(s[ty[i]][stru[j][2]])]); } } printf("%d\n",ans[len]+len); } return 0; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法