[AtCoder Regular Contest 109]-C - Large RPS Tournament(记忆化搜索)
[AtCoder Regular Contest 109]-C - Large RPS Tournament(记忆化搜索)
题目链接:https://atcoder.jp/contests/arc109/tasks/arc109_c
题面:
题意:
给定一个长度为\(\mathit n\)的字符串和一个正整数\(\mathit k\)。
问你\([0,2^k-1]\)选手们已经一场剪刀石头布的锦标赛的结果(只需要输出最后赢家的出拳类型)。
其中第\(\mathit i\)个选手每次出拳类型为\(s[((i\text{ mod } n) + 1)]\)。
对于一个区间\([l,r]\)的锦标赛,若\(r-l+1=2\)则两人通过正常的剪刀石头布规则决定胜负。
否则将区间分成\([l,mid],[mid+1,r]\)两个部分,其中\(mid=(l+r)/2\),将两个子区间的赢家进行比较胜负即可。
思路:
将字符串读入为下标为\(\text 0\)开始的,则第\(\mathit i\)个选手每次出拳类型为\(s[(i\text{ mod } n)]\),更方便写代码。
考虑记忆化搜索维护dp值:
\(dp[i][j]\)代表以\(\mathit i\)为区间的左端点,区间长度为\(2^j\)的区间的赢家出拳类型。
\(base_j=2^j mod\ n\)。
转移:
当\(j=0,dp[i][j]=s[i]\)
\(j>0,dp[i][j]=win(dp[i][j-1],dp[(i + base[j - 1]) % n][j - 1])\)
其中\(win(x,y)\)代表双方出拳为\(x,y\) 时的赢家类型。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
char s[maxn];
// R beats S;
// P beats R;
// S beats P.
//
int n, base[1010];
char dp[105][105];
char dfs(int pos, int k)
{
if (k == 0) {
return s[pos];
}
if (dp[pos][k] != '\0') {
return dp[pos][k];
}
char l = dfs(pos, k - 1);
char r = dfs((pos + base[k - 1]) % n, k - 1);
char res;
if (l == r) {
res = l;
} else {
if (r + l == 'R' + 'S') {
res = 'R';
}
if (r + l == 'R' + 'P') {
res = 'P';
}
if (r + l == 'S' + 'P') {
res = 'S';
}
}
dp[pos][k] = res;
return res;
}
int k;
int main()
{
#if DEBUG_Switch
freopen("D:\\code\\input.txt", "r", stdin);
#endif
//freopen("D:\\code\\output.txt","w",stdout);
n = readint();
k = readint();
scanf("%s", s);
base[0] = 1;
repd(i, 1, k) {
base[i] = base[i - 1] * 2 % n;
}
printf("%c\n", dfs(0, k) );
return 0;
}
本博客为本人原创,如需转载,请必须声明博客的源地址。
本人博客地址为:www.cnblogs.com/qieqiemin/
希望所写的文章对您有帮助。