早晨有點迷糊,讀錯題WA了一次,而後又CE了一次.....ios
給出一個字符串 均爲小寫字母 且 長度 <= 10ide
若給出的字符串可編碼則輸出對應數字,不然輸出0.編碼
可編碼的要求爲:字母的字典許從左到右一次增大。spa
設len爲字符串長度,i 爲對應字母,f[][] 爲 長度爲len 首字母爲 i 是 的狀況 ,則有:code
當 len = 1 時 f[1][i] = 1;blog
當 len = 2 時 f[2][i] = 26-i;ci
當 len >= 3 時 f[len][i] = f[len-1][i+1] +……+f[len-1][26-len+1];字符串
統計時先統計比給出字符串長度短的狀況,而後統計與其長度相等的狀況。string
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 7 #define LL unsigned long long 8 9 using namespace std; 10 11 char num[12]; 12 LL ans[12][28]; 13 14 void init() 15 { 16 LL i,j,k,sum; 17 18 for(i = 1; i <= 26; ++i) 19 ans[1][i] = 1; 20 for(i = 1; i <= 25; ++i) 21 ans[2][i] = 26 - i; 22 23 for(i = 3; i <= 10; ++i) 24 { 25 for(j = 1; j <= 27-i; ++j) 26 { 27 for(sum = 0,k = j+1; k <= 28-i; ++k) 28 { 29 sum += ans[i-1][k]; 30 } 31 ans[i][j] = sum; 32 } 33 } 34 } 35 36 int main() 37 { 38 init(); 39 cin>>num; 40 41 LL sum,tsum,len,i,j; 42 43 for(i = 1,len = strlen(num); i < len; ++i) 44 { 45 if(num[i] <= num[i-1]) 46 { 47 cout<<"0"<<endl; 48 return 0; 49 } 50 } 51 52 53 54 sum = 0; 55 56 for(i = 1,len = strlen(num); i < len; ++i) //統計小於len的狀況 57 { 58 for(j = 27-i; j >= 1; --j) 59 sum += ans[i][j]; 60 } 61 62 for(i = 0; i < len; ++i) 63 { 64 tsum = 0; 65 for(j = (i == 0 ? 1 : num[i-1] - 'a' + 2); j < num[i]-'a' + 1; ++j) 66 { 67 tsum += ans[len-i][j]; 68 } 69 70 sum += tsum; 71 } 72 ++sum; 73 cout<<sum<<endl; 74 75 return 0; 76 }