问题
HDU 6034 Balala Power! - https://acm.hdu.edu.cn/showproblem.php?pid=6034
分析
- 通过进制方式比较大小
- 任意一个字符串的首字符不能转换成 0 0 0
- 存在多次进位的可能,例如:n个等长的字符串且首字相同,以下3处代码与此有关
const int N = 100010;
for(int j = N; j > N-mxlen-6; --j){
for(int i = N - mxlen - 6; i <= N; ++i)
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
const int MXN = N + 15;
const int MD = 1e9 + 7;
int n, d[26][MXN], isf[26], mxlen, ans[26], f[MXN]={1};
char s[MXN];
int main(){
int t = 0;
for(int i = 1; i < MXN; ++i)
f[i] = 1LL*f[i-1]*26%MD;
while(scanf("%d", &n) == 1){
memset(d, 0, sizeof d);
memset(ans , 0, sizeof ans);
memset(isf, 0, sizeof isf);
mxlen = 0;
while(n--){
scanf("%s", s);
isf[s[0]-'a'] = 1;
int len = strlen(s);
if(mxlen < len) mxlen = len;
for(int i = len-1; i >= 0; --i) ++d[s[i]-'a'][N-len+1+i];
}
for(int i = 0; i < 26; ++i){
for(int j = N; j > N-mxlen-6; --j){
d[i][j-1] += d[i][j]/26, d[i][j] %= 26;
ans[i] = (ans[i] + 1LL*d[i][j]*f[N-j])%MD;
}
s[i] = i;
}
sort(s, s+26, [](int x, int y){
for(int i = N - mxlen - 6; i <= N; ++i) if(d[x][i] != d[y][i]) return d[x][i] < d[y][i];
return false;
});
for(int i = 0; i < 26; ++i){
if(isf[s[i]]) continue;
for(int j = i; j > 0; --j) s[j-1] ^= s[j], s[j] ^= s[j-1], s[j-1] ^= s[j];
break;
}
ans[s[0]] = 0;
for(int i = 1; i < 26; ++i) ans[s[0]] = (ans[s[0]] + 1LL*ans[s[i]]*i)%MD;
printf("Case #%d: %d\n", ++t, ans[s[0]]);
}
return 0;
}