多校1 B Balala Power!

来源:互联网 发布:辽宁网络棋牌频道直播 编辑:程序博客网 时间:2024/05/21 17:30

Balala Power!

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


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
 

这道题赛后做的时候,wa+tle不下20发,感觉细节很重要,题目主要分3部分,第一部分是给字母一个从0-25的权值,一开始很天真的想把所处不同位置的26的幂求和作为权值的排序标准,但显然会超出longlong的范围,其实用一个2维数组,行数代表字母,列代表指数,在某个位置出现就数组相应位置加1,满26就向前进一位。第二部分就是赋予权值,这一部分我一直是用结构体排序的,总是超时,后来是想到可以用一个数组来根据另一个数组的内容来排序的方法,可以节省很多时间,这里要注意排序完之后,要给最小并且在首字母位置没有出现过的字母记录权值为0。第三部分是求和,从头到尾把二维数组求和,开始是从左往右从上往下遍历的,结果超时,因为将26的幂重复算了很多遍,但是如果是先从上往下,再从左往右,那么就可以只计算26的幂一次就可以了。



#include<iostream>#include<cmath>#include<cstdio>#include<cstring>#include<algorithm> using namespace std;const long long int MOD=7+1e9;int flag[27];int dis[27];int s[27][100011];bool cmp1(int x,int y){    for(int i=100009;i>=0;i--)    {        if(s[x][i]>s[y][i])return 1;        if(s[x][i]<s[y][i])return 0;    }    return 0;}int main(){    int n;    int cas1=0;    while(~scanf("%d",&n))    {        memset(flag,0,sizeof(flag));        memset(dis,-1,sizeof(dis));        memset(s,0,sizeof(s));        int i;        char a[111111];        for(i=1;i<=n;i++)        {            cin>>a;            if(strlen(a)>1)flag[a[0]-'a'+1]=1;            for(int j=0;j<strlen(a);j++)            s[a[j]-'a'+1][strlen(a)-1-j]++;        }        for(i=1;i<=26;i++)        {            for(int j=0;j<=100002;j++)            {                if(s[i][j]>=26)                {s[i][j+1]+=s[i][j]/26;                    s[i][j]%=26;                                    }            }        }            int ss[27];            for(i=1;i<=26;i++)            ss[i]=i;            sort(ss+1,ss+1+26,cmp1);            int kk=25;            for(i=26;i>=1;i--)            {                if(flag[ss[i]]!=1)                {                    dis[ss[i]]=0;                    break;                }            }            for(i=1;i<=26;i++)            {                if(dis[ss[i]]==-1)                {                dis[ss[i]]=kk--;            }                //cout<<ss[i]<<endl;            }            long long int ans=0;            long long int er6=1;            for(i=0;i<=100002;i++)            {                                      for(int j=1;j<=26;j++)                    {                                                ans=(ans+(s[j][i]%MOD)*(er6*dis[j])%MOD)%MOD;                    }                    er6*=26;                    er6%=MOD;            }            cout<<"Case #"<<++cas1<<": ";        cout<<ans<<endl;    }    return 0;} 



原创粉丝点击