POJ 2033 Alphacode

來源:互聯網
上載者:User

Description

Alice and Bob need to send secret messages to each other and are discussing ways to encode their messages:

Alice: "Let's just use a very simple code: We'll assign 'A' the code word 1, 'B' will be 2, and so on down to 'Z' being assigned 26."
Bob: "That's a stupid code, Alice. Suppose I send you the word 'BEAN' encoded as 25114. You could decode that in many different ways!”
Alice: "Sure you could, but what words would you get? Other than 'BEAN', you'd get 'BEAAD', 'YAAD', 'YAN', 'YKD' and 'BEKD'. I think you would be able to figure out the correct decoding. And why would you send me the word ‘BEAN’ anyway?”
Bob: "OK, maybe that's a bad example, but I bet you that if you got a string of length 500 there would be tons of different decodings and with that many you would find at least two different ones that would make sense."
Alice: "How many different decodings?"
Bob: "Jillions!"

For some reason, Alice is still unconvinced by Bob's argument, so she requires a program that will determine how many decodings there can be for a given string using her code.

Input

Input will consist of multiple input sets. Each set will consist of a single line of digits representing a valid encryption (for example, no line will begin with a 0). There will be no spaces between the digits. An input line of '0' will terminate the input and should not be processed

Output

For each input set, output the number of possible decodings for the input string. All answers will be within the range of a long variable.

Sample Input

25114111111111133333333330

Sample Output

6891
#include<iostream>#include<cstring>using namespace std;//利用遞推和動態規劃中的狀態記錄思想,從右往左遞推//開DP二維數組將多種狀態儲存//DP[0][I]儲存的是到這個數位它單個數位與其後面數字結合的情況數,DP[0][I] = DP[2][I+1]//DP[1][I]儲存的是從右到這個數位向右結合的情況總數,這一情況如果滿足,即temp<=26,則繼承在它之後2位的情況總數 DP[1][I] = DP[2][I+2]//DO[2][I]儲存的是單一數位和雙數位結合這兩種情況的總數 DP[2][I] = DP[0][I] + DP[I]//此題尤其要注意 0 的結合情況,如120,1201,因為資料都是合理,不存在100,1002等,因此需要分類討論//如果當前數位是0,那麼他不存在與後結合的情況,因此DP[1][I]=0,並且前面數位一定非0,因為是合理資料//如果當前數位不是0,判斷是否能組成雙數位(<=26)//如果當前數位不是0,但後面數位只存在雙數位情況,如120,110,那麼,當前數位不能和後一數位結合,DP[1]=0int main(){char a[8000];int dp[3][8000];//題意中的lenth 500很賤,並不意味著最大輸入上限是500,其實資料輸入的長度可以很長,建議開大,8000就足夠了while(cin >> a){int l = strlen(a);int temp;if(a[0] == '0')break;memset(dp,0,sizeof(dp));dp[2][l] = 1;if(a[l - 1] == '0'){dp[0][l - 1] = 0;dp[1][l - 1] = 0;dp[2][l - 1] = dp[0][l - 1] + dp[1][l - 1];}else{dp[0][l - 1] = 1;dp[1][l - 1] = 0;dp[2][l - 1] = dp[0][l - 1] + dp[1][l - 1];}for(int i = l - 2;i >= 0; --i){dp[0][i] = dp[2][i + 1];temp = (a[i] - '0') * 10 + a[i + 1] - '0';if(a[i] == '0'){dp[1][i] = 0;dp[2][i] = dp[0][i] + dp[1][i];}else{if(temp <= 26 && a[i + 1] != '0' && a[i + 2] != '0'){dp[1][i] = dp[2][i + 2];dp[2][i] = dp[0][i] + dp[1][i];}else if(temp <= 26 && a[i + 1] != '0' && a[i + 2] == '0'){dp[1][i] = 0;dp[2][i] = dp[0][i] + dp[1][i];}else if(a[i + 1] == '0'){if(dp[2][i + 1] != 0)dp[2][i] = dp[2][i + 1];else dp[2][i] = 1;}else if(a[i + 1] != '0'){dp[1][i] = 0;dp[2][i] = dp[0][i] + dp[1][i];}}}cout << dp[2][0] << endl;}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.