InputThe input contains multiple test cases.
For each test case, the first line contains one positive integers nn, the number of strings. (1≤n≤100000)(1≤n≤100000)
Each of the next nn lines contains a string sisi consisting of only lower case letters.(1≤|si|≤100000,∑|si|≤106)(1≤|si|≤100000,∑|si|≤106)
OutputFor each test case, output " Case #xx: yy" in one line (without quotes), where xxindicates the case number starting from 11 and yy denotes the answer of corresponding case.Sample Inputios
1 a 2 aa bb 3 a ba abc
Sample Output數組
Case #1: 25 Case #2: 1323 Case #3: 18221
題目大意:給出n行小寫字母組成的字符串,字符串的每一個字母有26進製表示而且字符串的第一個字符不能爲0,求這n行字符串相加的和是多少??
解題思路:首先使用二維數組記錄字符串的位置上的字符和一維數組第一個字符進行統計,而後用二維數組對結構體進行排序,從大到小將不是前導的字符賦值爲0,從小到大對不是0的字符由25開始往0對字符進行賦值,最後對於二維數組進行計數,打印結果便可。
AC代碼:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 const int eps=1e9+7; 8 long long a[100005][30],flag[100005]; 9 char s[100005]; 10 long long ans[30]; 11 int step,num[30],cnt[30]; 12 int cmp(int A,int B) 13 { 14 for(int i=step;i>=0;i--) 15 { 16 if(a[i][A]!=a[i][B]) 17 return a[i][A]<a[i][B]; 18 } 19 return A<B; 20 } 21 int main() 22 { 23 flag[0]=1; 24 for(int i=1;i<100003;i++) 25 flag[i]=(long long)(flag[i-1]*26)%eps; 26 int n,cas=0; 27 //freopen("1002.in","r",stdin); 28 //freopen("1002.out","w",stdout); 29 while(~scanf("%d",&n)) 30 { 31 cas++; 32 memset(ans,0,sizeof(ans)); 33 memset(cnt,0,sizeof(cnt)); 34 memset(a,0,sizeof(a)); 35 step=0; 36 for(int i=0;i<n;i++) 37 { 38 scanf("%s",s); 39 int len=strlen(s); 40 if(len>1) 41 cnt[s[0]-'a']=1; 42 reverse(s,s+len); 43 for(int j=0;j<len;j++) 44 { 45 a[j][s[j]-'a']++; 46 } 47 step=max(step,len); 48 } 49 for(int i=0;i<26;i++) 50 { 51 for(int j=0;j<step;j++) 52 { 53 a[j+1][i]+=a[j][i]/26; 54 a[j][i]=a[j][i]%26; 55 } 56 while(a[step][i]) 57 { 58 a[step+1][i]+=a[step][i]/26; 59 a[step][i]=a[step][i]%26; 60 step++; 61 } 62 num[i]=i; 63 } 64 sort(num,num+26,cmp); 65 int zero=-1; 66 for(int i=0;i<26;i++) 67 { 68 if(!cnt[num[i]]) 69 { 70 zero=num[i]; 71 break; 72 } 73 } 74 int k=25; 75 for(int i=25;i>=0;i--) 76 { 77 if(zero!=num[i]) 78 { 79 ans[num[i]]=k; 80 k--; 81 } 82 } 83 long long key=0; 84 for(int i=0;i<step;i++) 85 { 86 for(int j=0;j<26;j++) 87 { 88 key=(key+(ans[j]*flag[i]*a[i][j]))%eps; 89 } 90 } 91 printf("Case #%d: %lld\n",cas,key); 92 } 93 return 0; 94 }