题意:
给你一个长度2000的字符串。然后每个人轮流每次从字符串的头或者尾取一个字符,然后添加到自己的字符串的前面。如果字符拿完了就停止,如果谁的字符字典序小谁赢,输出谁赢,或者平局。
思考:
代码:
#include
#define fi first
#define se second
#define pb push_back
#define db double
#define int long long
#define PII pair<int,int >
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int mod = 1e9+7,inf = 1e18;
const int N = 2e5+10,M = 2010;
int T,n,m,k;
int va[N];
char s[N];
int dp[M][M];
void solve()
{
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++) dp[i][j] = 0;
}
for(int i=1;i<n;i++)
{
if(s[i]!=s[i+1]) dp[i][i+1] = 1;
}
for(int len=3;len<=n;len++)
{
for(int i=1;i+len-1<=n;i++)
{
int j = i+len-1;
int suc1 = 0,suc2 = 0,res = 0;
res = 0;
if((dp[i+1][j-1]==0&&s[i]<s[j])||dp[i+1][j-1]==1) res++;
if((dp[i+2][j]==0&&s[i]<s[i+1])||dp[i+2][j]==1) res++;
if(res>=2) suc1 = 1;
res = 0;
if((dp[i+1][j-1]==0&&s[j]<s[i])||dp[i+1][j-1]==1) res++;
if((dp[i][j-2]==0&&s[j]<s[j-1])||dp[i][j-2]==1) res++;
if(res>=2) suc1 = 1;
res = 0;
if((dp[i+1][j-1]==0&&s[i]>s[j])||dp[i+1][j-1]==2) res++;
if((dp[i+2][j]==0&&s[i]>s[i+1])||dp[i+2][j]==2) res++;
if(res>=2) suc2++;
res = 0;
if((dp[i+1][j-1]==0&&s[j]>s[i])||dp[i+1][j-1]==2) res++;
if((dp[i][j-2]==0&&s[j]>s[j-1])||dp[i][j-2]==2) res++;
if(res>=2) suc2++;
if(suc1) dp[i][j] = 1;
else if(suc2==2) dp[i][j] = 2;
else dp[i][j] = 0;
}
}
if(dp[1][n]==1) cout<<"Alice\n";
if(dp[1][n]==0) cout<<"Draw\n";
if(dp[1][n]==2) cout<<"Bob\n";
}
signed main()
{
IOS;
cin>>T;
while(T--)
{
cin>>s+1;n = strlen(s+1);
solve();
}
return 0;
}
总结:
多tm思考思考,把所学的知识结合起来,多动手看看,翻翻博客。