Balala Power!
Talented Mr.Tang has
n
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
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
109+7.
For each test case, the first line contains one positive integers n n , the number of strings. (1≤n≤100000) (1≤n≤100000)
Each of the next n n lines contains a string si si consisting of only lower case letters. (1≤|si|≤100000,∑|si|≤106) (1≤|si|≤100000,∑|si|≤106)
1 a 2 aa bb 3 a ba abc
Case #1: 25 Case #2: 1323 Case #3: 18221
官方题解:每个字符对答案的贡献都可以看作一个 26 进制的数字,问题相当于要给这些贡献加一个 0 到 25 的权重使得答案最大。最大的数匹配 25,次大的数匹配 24,依次类推。排序后这样依次贪心即可,唯一注意的是不能出现前导 0。
反正我是看别人代码然后敲了好长时间,自己写的时候各种问题已然无解。
结构体里数组存每个字母所在位置的个数,然后26进制当然是进位了,
sort的时候注意顺序。
然后就是前导零的问题,当字符串只有一个字符的时候可以出现前导零,否则就必须标记这个字符,
最后在遍历的时候找到可以赋值为零的字符,并给这个字符前面和后面的值进行更改
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define maxn 100000+100 using namespace std; const int MOD=1000000007; typedef long long ll; struct Node{ int a[maxn]; int pos; }A[30]; bool vis[30]; char s[maxn]; bool cmp(Node &m,Node &n) { for(int i=maxn-1;i>0;i--) { if(m.a[i]!=n.a[i]) return m.a[i]<n.a[i]; } return m.a[0]<n.a[0]; } int main() { int kase=1; int n; while(scanf("%d",&n)!=EOF) { memset(vis,false,sizeof vis); for(int i=0;i<26;i++) { A[i].pos=i; for(int j=0;j<maxn;j++) A[i].a[j]=0; } for(int i=0;i<n;i++) { scanf("%s",s); int len=strlen(s)-1; if(len>0) vis[s[0]-'a']=true; for(int j=0;j<=len;j++) { int x=s[j]-'a'; int y=len-j; A[x].a[y]++; while(A[x].a[y]==26) { A[x].a[y++]=0; A[x].a[y]++; } } } sort(A,A+26,cmp); int temp=-1; for(int i=0;i<26;i++) { if(!vis[A[i].pos]) { temp=i; break; } } ll tab,ans=0,mul; for(int i=0;i<26;i++) { tab=0; mul=0; if(i==temp) mul=0; else if(i<temp) mul=i+1; else mul=i; for(int j=maxn-1;j>=0;j--) { tab=(tab*26)%MOD; tab=(tab+(ll)A[i].a[j]*mul)%MOD; } ans=(ans+tab)%MOD; } printf("Case #%d: %lld\n",kase++,ans); } return 0; }