标签:std div class strlen str sum ase size namespace
/*hdu6034[模拟] 2017多校1*/ //暴力模拟26个26进制数即可, 要注意进位 #include<bits/stdc++.h> using namespace std; typedef long long LL; const double eps=1e-8; const int inf=0x3f3f3f3f; struct node{ char num[100005]; int ch,high; }bits[30]; const int mod=1000000007; int n,len,pos,mark[30],w[30],kase=1; char str[100005]; LL fastpow(LL a,LL b){ LL ret=1,x=a; while(b){ if(b&1){ ret=(ret*x)%mod; } x=(x*x)%mod; b>>=1; } return ret; } bool cmp(node a,node b){ if(a.high!=b.high){ return a.high>b.high; } else{ for(int i=a.high-1;i>=0;i--){ if(a.num[i]>b.num[i]){ return 1; } else if(a.num[i]<b.num[i]){ return 0; } } } return 0; } LL cal(node& a){ LL ret=0; for(int i=0;i<a.high;i++){ if(a.num[i]){ ret=(ret+((LL)a.num[i]*fastpow(26LL,i))%mod)%mod; } } return ret; } void solve(){ LL sum=0; sort(bits,bits+26,cmp); for(int i=0;i<26;i++){ if(bits[i].high) w[bits[i].ch]=25-i; if(mark[bits[i].ch]&&w[bits[i].ch]==0){ int pos=i; while(mark[bits[pos].ch]&&pos) pos--; for(int j=pos+1;j<26;j++) w[bits[j].ch]++; w[bits[pos].ch]=0; } } for(int i=0;i<26;i++){ sum=(sum+w[bits[i].ch]*cal(bits[i]))%mod; } printf("Case #%d: %lld\n",kase++,sum); } int main(){ while(~scanf("%d",&n)){ memset(bits,0,sizeof(bits)); memset(w,0,sizeof(w)); memset(mark,0,sizeof(mark)); for(int i=0;i<26;i++) bits[i].ch=i; for(int i=0;i<n;i++){ scanf("%s",str); len=strlen(str); mark[str[0]-‘a‘]++; for(int i=0;i<len;i++){ pos=len-i-1; bits[str[i]-‘a‘].high=max(bits[str[i]-‘a‘].high,pos+1); bits[str[i]-‘a‘].num[pos]++; while(pos<99999&&bits[str[i]-‘a‘].num[pos]>=26){ int k=bits[str[i]-‘a‘].num[pos]/26; bits[str[i]-‘a‘].num[pos]-=26; bits[str[i]-‘a‘].num[++pos]++; }bits[str[i]-‘a‘].high=max(bits[str[i]-‘a‘].high,pos+1); } }solve(); } return 0; }
标签:std div class strlen str sum ase size namespace
原文地址:http://www.cnblogs.com/UnderSilenceee/p/7295987.html