HDU6034-Balala Power!

本文介绍了一种通过重新映射字符值来最大化多个字符串在26进制下的总和的方法。通过对字符出现频率的统计和排序,确保高位权重最大的字符对应数值也最大,从而实现最优加和。

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

Balala Power!

                                                                    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
                                                                                            Total Submission(s): 3437    Accepted Submission(s): 800


Problem Description

Talented  Mr.Tang has  n  strings consisting of only lower case characters. He wants to charge them with Balala Power (he could change each character ranged from  a to  z into each number ranged from  0 to  25, but each two different characters should not be changed into the same number) so that he could calculate the sum of these strings as integers in base  26  hilariously.

Mr.Tang wants you to maximize the summation. Notice that no string in this problem could have leading zeros except for string "0". It is guaranteed that at least one character does not appear at the beginning of any string.

The summation may be quite large, so you should output it in modulo  109+7 .
 

Input
The input contains multiple test cases.

For each test case, the first line contains one positive integers  n , the number of strings.  (1n100000)

Each of the next  n  lines contains a string  si  consisting of only lower case letters.  (1|si|100000,|si|106)
 

Output
For each test case, output " Case # x y " in one line (without quotes), where  x  indicates the case number starting from  1  and  y  denotes the answer of corresponding case.
 

Sample Input
  
1 a 2 aa bb 3 a ba abc
 

Sample Output
  
Case #1: 25 Case #2: 1323 Case #3: 18221
 

Source
 

题意:给你n个字符串,每个串只包含小写字母,用0~25去代替这些字母,问字符串加起来最大为多少,结果转化为10进制,并且字符串不能有前导零(只有一个字符可以为零)

解题思路:计算出每个字符在每一位出现的次数,然后根据每个字符在每位出现的次数进行排序,最高位出现最多的必然最大,然后一个个排下来即可,前导零只要一开始做一下标记,然后不能为零的就和前面的进行交换即可(这题还要注意进位,即一次统计出来的并不是真正的每个字符出现的次数,还会存在进位的影响,不过最后进位后若字符串长度变长了可以不用继续进行进位,因为最高位不会再出现影响)



#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;

int n, ma, vis[30], ans[30];
string s[100009];
struct node
{
    int id, sum[100009];
    friend bool operator <(const node &a, const node &b)
    {
        for (int i = ma-1; i>=0; i--)
            if (a.sum[i] != b.sum[i]) return a.sum[i]>b.sum[i];
    }
} x[30];

int main()
{
    int cas = 0;
    while (~scanf("%d", &n))
    {
        ma = -1;
        memset(vis, 0, sizeof vis);
        for (int i = 1; i <= n; i++)
        {
            cin >> s[i], ma = max(ma, (int)s[i].length());
            if (s[i].length() != 1) vis[s[i][0] - 'a' + 1] = 1;
        }
        memset(x, 0, sizeof x);
        for (int i = 1; i <= n; i++)
        {
            int len = s[i].length();
            for (int j = len-1; j>=0; j--)
                x[s[i][j] - 'a' + 1].sum[len-1-j]++, x[s[i][j] - 'a' + 1].id = s[i][j] - 'a' + 1;
        }
        for (int i = 1; i < 27; i++)
        {
            for (int j = 0; j <ma; j++)
            {
                if (x[i].sum[j] >= 26)
                {
                    x[i].sum[j + 1] += x[i].sum[j] / 26;
                    x[i].sum[j] %= 26;
                }
            }
        }
        for(int i=1;i<27;i++)
            if(x[i].sum[ma]) {ma++;break;}
        sort(x + 1, x + 27);
        int cnt = 25;
        for (int i = 1; i <= 26; i++)
        {
            ans[x[i].id] = cnt--;
            if (i == 26)
            {
                int k = i;
                while (vis[x[k].id])
                {
                    swap(ans[x[k].id], ans[x[k - 1].id]);
                    k--;
                }
            }
        }
        LL ans1 = 0;
        for (int i = 1; i <= n; i++)
        {
            int len = s[i].length();
            LL sum = 0;
            for (int j = 0; j<len; j++)
                sum = (sum * 26 + ans[s[i][j] - 'a' + 1]) % mod;
            ans1 += sum;
            ans1 %= mod;
        }
        printf("Case #%d: %lld\n", ++cas, ans1);
    }
    return 0;
}

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值