POJ 1850 Code
来源:互联网 发布:js发送http请求 cookie 编辑:程序博客网 时间:2024/06/05 11:40
早晨有点迷糊,读错题WA了一次,然后又CE了一次.....
给出一个字符串 均为小写字母 且 长度 <= 10
若给出的字符串可编码则输出对应数字,否则输出0.
可编码的要求为:字母的字典许从左到右一次增大。
设len为字符串长度,i 为对应字母,f[][] 为 长度为len 首字母为 i 是 的情况 ,则有:
当 len = 1 时 f[1][i] = 1;
当 len = 2 时 f[2][i] = 26-i;
当 len >= 3 时 f[len][i] = f[len-1][i+1] +……+f[len-1][26-len+1];
统计时先统计比给出字符串长度短的情况,然后统计与其长度相等的情况。
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 }
- poj 1850 Code
- poj 1850 Code
- poj 1850 code
- poj 1850 Code
- poj 1850 Code
- poj 1850 Code
- poj 1850 Code
- POJ 1850 Code
- POJ 1850 Code
- poj 1850 Code
- POJ 1850 Code
- poj 1850 Code
- POJ 1850 Code
- POJ 1850 Code
- POJ 1850--Code
- POJ 1850 Code
- POJ - 1850 Code
- POJ 1850 Code
- HDU 1396 Counting Triangles
- poj 2773 Happy 2006
- HDU 1722 Cake
- UVa11401 Triangle Counting
- POJ 3252 Round Numbers
- POJ 1850 Code
- gcc链接,去掉不用的函数和data
- POJ 1942 Paths on a Grid 组合数的优化
- Documentation-filesystems/sysfs
- HDU 1009 The Shortest Path in Nya Graph
- HDU 4714 Tree2Cycle
- Nginx的基本操作
- 随机抽样一致性算法(RANSAC)
- iOS百度地图——用户当前位置