HDU6034 Balala Power!(思路+大数)

来源:互联网 发布:韩服数据 编辑:程序博客网 时间:2024/05/22 15:12

Balala Power!

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


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 #xy" in one line (without quotes), where x indicates the case number starting from 1 and y denotes the answer of corresponding case.
 

Sample Input
1a2aabb3abaabc
 

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

    题意:给出 n个字符串,字符包括 a~z,分别给它们赋值 0~25,使得这些串表达成26进制时的和最大
求出每个位置每个字母的个数,倒着存,此时就有大数的思想,相当于一个26进制的大数,首先要对获得的大数进位,满26进1
然后比较每个字母的贡献,重载<,用sort对获得的大数排序,依次赋值
最后考虑前导0的情况,找到第一个最小的不能为前导0的位置,把该位置变为0,循环左移即可
#include<stdio.h>#include<string.h>#include<iostream>#include<string>#include<algorithm>#define mem(a,b) memset(a,b,sizeof(a))using namespace std;typedef long long ll;const int N=100100;const int MOD=1e9+7;const int M=30;struct node{    int num[N];    int id;    bool operator < (const node&a)const    {        for(int i=100000;i>=0;i--)        {            if(num[i]!=a.num[i])                return num[i]>a.num[i];        }        return 0;    }}e[M];bool vis[M];ll num[M],re[N];string s[N];int main(){    ios::sync_with_stdio(false);    re[0]=1;    for(int i=1;i<N;i++)    {        re[i]=re[i-1]*26;        re[i]%=MOD;    }    int n,q=0;    while(cin>>n)    {        for(int i=0;i<26;i++)        {            for(int j=0;j<=100000;j++)                e[i].num[j]=0;            e[i].id=i;            vis[i]=0;        }        for(int i=0;i<n;i++)        {            cin>>s[i];            int lenn=s[i].length();            for(int j=0,k=lenn-1;j<lenn;j++,k--)            {                int t=s[i][k]-'a';                e[t].num[j]++;            }            if(lenn>1)vis[s[i][0]-'a']=1;        }        for(int i=0;i<26;i++)        {            for(int j=0;j<=100000;j++)            {                e[i].num[j+1]+=e[i].num[j]/26;                e[i].num[j]%=26;            }        }            sort(e,e+26);            for(int i=25;i>=0;i--)            {               num[e[25-i].id]=i;            }            int t=25;            while(vis[e[t].id]&&t)            {                swap(num[e[t].id],num[e[t-1].id]);                t--;            }            ll ans=0;           for(int i=0;i<n;i++)           {                  int len=s[i].length();               for(int j=0;j<len;j++)               {                   ans=(ans+num[s[i][j]-'a']*re[len-1-j]%MOD)%MOD;               }           }           cout<<"Case #"<<++q<<": "<<ans<<endl;    }}



原创粉丝点击