多校1002 HDU-6034 Balabala Power-26进制大数加&贪心

题目链接:HDU-6034
大意:
给一堆字符串,每个字符串分配一个权值,把每个字符串变成26进制数,求数字和最大。
要求不能有前导 0,即首字母不为 0 。

思路:
每个字符串相加,得到一个很长的数组,每个位置记录这个位置的值(字母和其出现次数),即 int [MAXN][26] 其值表示个数。
将其看成一个大数,对其进行进位处理,然后排序,从高到底赋值,并且要避免前导 0 。
避免前导 0 的具体做法是,从小的开始赋值,遇到非首位的即赋为 0 ,再从大开始一一赋值。
代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mem(s,t) memset(s,t,sizeof(s))
#define D(v) cout<<#v<<" "<<v<<endl
#define inf 0x3f3f3f3f
//#define LOCAL
const ll MOD = 1000000007;
ll mp[150005][26];
int n, m;
char s[100005];
bool notz[26];
int val[26], a[26];
void up() {
    for (int j = 0; j < 26; j++) {
        for (int i = 0; i < m; i++) {
            mp[i+1][j] += mp[i][j] / 26;
            mp[i][j] %= 26;
        }
        while (mp[m][j]) {
            mp[m+1][j] += mp[m][j] / 26;
            mp[m][j] %= 26;
            m++;
        }
    }
}
bool cmp(int a, int b) {
    for (int i = m-1; i >= 0; i--) {
        if (mp[i][a] != mp[i][b]) return mp[i][a] > mp[i][b];
    }
    return 0;
}
void solve() {
    for (int i = 0; i < 26; i++) a[i] = i;
    sort(a, a+26, cmp);
    for (int i = 25; i >= 0; i--)
        if (!notz[a[i]]) {
            val[a[i]] = 0;
            break;
        }
    int tmp = 25;
    for (int i = 0; i < 26; i++)
        if (val[a[i]] == -1) val[a[i]] = tmp--;
}
int main() {
    int tt = 0;
    while (~scanf("%d", &n)) {
        mem(notz, 0);
        mem(mp, 0);
        mem(val, -1);
        m = 0;
        for (int i = 1; i <= n; i++) {
            scanf("%s", s);
            int len = strlen(s);
            if (len != 1) notz[s[0]-'a'] = 1;
            m = max(m, len);
            for (int i = 0; i < len; i++) mp[len-i-1][s[i]-'a']++;
        }
        up();
        solve();
        ll ans = 0;
        for (int i = m-1; i >= 0; i--) {
            ans = ans * 26 % MOD;
            for (int j = 0; j < 26; j++) {
                ans = (ans + (ll)mp[i][j]*val[j] % MOD) % MOD;
            }
        }
        printf("Case #%d: %lld\n", ++tt, ans % MOD);
    }
}

PS.自己打老是出错,不知道怎么回事,疯狂wa近乎绝望,模拟太差,此代码搬运自学长博客 nicetomeetu

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 引用\[1\]和引用\[2\]是关于解决HDU - 3853问题的AC代码。这段代码使用了动态规划的思路,通过计算每个点的期望能量来解决问题。其中,f\[i\]\[j\]表示走到(i, j)这个点的期望能量。代码中还有一些特殊情况的处理,比如停留在原地的花费为2点,停留在原地的概率为1的情况下期望能量为0。\[1\]\[2\] 引用\[3\]是关于Akemi Homura的描述,她是一个魔法少女,想要帮助她的朋友Madoka拯救世界。但由于Boss孵化器的情节,她被困在一个叫做LOOPS的迷宫中。\[3\] 所以,问题A - LOOPS HDU - 3853是关于解决迷宫问题的一个题目。 #### 引用[.reference_title] - *1* *3* [期望DP&mdash;&mdash;C - LOOPS HDU - 3853](https://blog.csdn.net/GreyBtfly/article/details/80534173)[target=&quot;_blank&quot; data-report-click={&quot;spm&quot;:&quot;1018.2226.3001.9630&quot;,&quot;extra&quot;:{&quot;utm_source&quot;:&quot;vip_chatgpt_common_search_pc_result&quot;,&quot;utm_medium&quot;:&quot;distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt&quot;}} ] [.reference_item] - *2* [HDU 3853-LOOPS【期望DP】](https://blog.csdn.net/qq_41785863/article/details/102570840)[target=&quot;_blank&quot; data-report-click={&quot;spm&quot;:&quot;1018.2226.3001.9630&quot;,&quot;extra&quot;:{&quot;utm_source&quot;:&quot;vip_chatgpt_common_search_pc_result&quot;,&quot;utm_medium&quot;:&quot;distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt&quot;}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值