您的位置:首页 > 其它

poj_1850 Code(组合数学/dfs)

2016-11-28 22:49 323 查看
Code
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 9523 Accepted: 4552
DescriptionTransmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system is that one where a number is associated to a character sequence. It is consideredthat the words are made only of small characters of the English alphabet a,b,c, ..., z (26 characters). From all these words we consider only those whose letters are in lexigraphical order (each character is smaller than the next character).The coding system works like this:• The words are arranged in the increasing order of their length.• The words with the same length are arranged in lexicographical order (the order from the dictionary).• We codify these words by their numbering, starting with a, as follows:a - 1b - 2...z - 26ab - 27...az - 51bc - 52...vwxyz - 83681...Specify for a given word if it can be codified according to this coding system. For the affirmative case specify its code.InputThe only line contains a word. There are some constraints:• The word is maximum 10 letters length• The English alphabet has 26 characters.OutputThe output will contain the code of the given word, or 0 if the word can not be codified.Sample Input
bf
Sample Output
55
一开始不知道下面这个公式(1)
所以用dfs暴力推,水过了。
看了网上的题解后才知道怎么用组合数学做。
//dfs#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 2000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;int num[12];char s[12], init[12];int ans;int len;void dfs(int pos){if(pos == len){if(len == 1) ans += s[pos] - 'a' + 1;else ans += s[pos]- s[pos-1];return ;}char t = s[pos], st;if(pos > 1) st = s[pos-1]+1;else st = 'a';for(char c = st; c < s[pos+1]; c++){s[pos] = c;dfs(pos+1);s[pos] = t;}}void dfs2(int pos){if(pos == len){if(len == 1) { ans += s[pos] - 'a' + 1; return ; }int k = 0;for(k = 1; k <= pos-1; k++) if(s[k] != init[k]) break;if(k == pos) ans += s[pos]- s[pos-1];else ans += 'z' - s[pos-1];return ;}char t = s[pos];if(pos > 1){int k = 0;for(k = 1; k <= pos-1; k++) if(s[k] != init[k]) break;char en;if(k == pos) en = s[pos];else en = 'z' - (len - pos);for(char c = s[pos-1]+1; c <= en; c++){s[pos] = c;dfs2(pos+1);s[pos] = t;}}else{for(char c = 'a'; c <= s[pos]; c++){s[pos] = c;dfs2(pos+1);s[pos] = t;}}}int main(){//FOP2;for(len = 1; len <= 10; len++){ans = 0;for(int i = len; i >= 1; i--) s[i] = 'z' - (len - i);dfs(1);num[len] = ans;}while(~scanf("%s", s+1)){ans = 0;strcpy(init+1, s+1);len = strlen(s+1);int i;for(i = 1; i < len; i++) if(s[i] >= s[i+1]) break;if(i != len){ printf("0\n"); continue; }for(i = 1; i < len; i++) ans += num[i];dfs2(1);printf("%d\n", ans);}return 0;}
//组合数学#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;int C[50][50];char s[12];int len;int main(){int i; char c;C[0][0] = 1;for(int i = 1; i <= 30; i++){C[i][0] = C[i][i] = 1; //边界条件for(int j = 1; j < i; j++)C[i][j] = (C[i-1][j] + C[i-1][j-1]) % mod;}while(~scanf("%s", s+1)){int ans = 0;len = strlen(s+1);for(i = 1; i < len; i++) if(s[i] >= s[i+1]) break;if(i != len){ printf("0\n"); continue; }for(i = 1; i < len; i++) ans += C[26][i];for(i = 1; i <= len; i++){if(i == 1) c = 'a';else c = s[i-1] + 1;for(; c < s[i]; c++) ans += C['z' - c][len - i];}printf("%d\n", ans+1);}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: