Hdu 6032 关于字符串的博弈

本文介绍了一个基于字符串操作的小型博弈论游戏算法实现。通过使用C++中的map和set等数据结构来高效处理游戏状态,文章详细展示了如何通过递归深度优先搜索(DFS)策略来确定最优解,同时考虑了玩家的得分和胜负情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

就是比较正常的博弈,因为数据量很小,写得很暴力也能过,map和set套一下还是很方便的。

#include <bits/stdc++.h>
using namespace std;
const int INF=1e9;
char s[35][35];
int n;

struct node{
    int win;//是否获胜
    int mys;//我的分数
    int ops;//对手分数
};

bool operator==(const node&a, const node& b){
    return a.win==b.win&&a.mys==b.mys&&a.ops==b.ops;
}

bool operator<(const node&a, const node& b){
    if(a.win!=b.win)return a.win<b.win;
    if(a.ops!=b.ops)return a.ops>b.ops;
    return a.mys<b.mys;
}

map<string, set<int> >mst;//统计每个子串在字典中出现的id的集合
map<string, int>mp;//score
map<string, node>ans;//ans

node dfs(string str){
    if(ans.find(str)!=ans.end())return ans[str];
    node M=(node){1, INF, 0};
    for(int i=0;i<26;i++){
        char p=i+'a';
        string add="";
        add+=p;
        string to1=add+str;
        if(mp.find(to1)!=mp.end()){
            node rev1=dfs(to1);
            if(rev1<M)M=rev1;
        }
        string to2=str+add;
        if(mp.find(to2)!=mp.end()){
            node rev2=dfs(to2);
            if(rev2<M)M=rev2;
        }
    }
    if(M==(node){1, INF, 0})return ans[str]=(node){0, 0, mp[str]};
    else return ans[str]=(node){M.win^1, M.ops, M.mys+mp[str]};
}

int main()
{

    while(~scanf("%d", &n)){
        mp.clear();
        mst.clear();
        ans.clear();
        for(int i=1;i<=n;i++)scanf("%s", s[i]+1);
        for(int i=1;i<=n;i++){
            int len=strlen(s[i]+1);
            for(int j=1;j<=len;j++){
                string str="";
                int sum=0, ma=0;
                for(int k=j;k<=len;k++){
                    str+=s[i][k];
                    mst[str].insert(i);
                    sum+=s[i][k]-'a'+1;
                    ma=max(s[i][k]-'a'+1, ma);
                    mp[str]=sum*ma;
                }
            }
        }
        for(auto it=mp.begin();it!=mp.end();it++){
            string str=it->first;
            it->second+=mst[str].size();
        }
        string str="";
        node ansn=dfs(str);
        if(ansn.win){
            printf("Alice\n");
        }
        else printf("Bob\n");
        printf("%d %d\n", ansn.mys, ansn.ops);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值