POJ3073Spam题解动态规划DP

Spam
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 835   Accepted: 470

Description

To thwart content-based spam filters, spammers often modify the text of a spam email to prevent its recognition by automatic filtering programs. For any plain text string s (containing only upper-case letters), let Φ(s) denote the string obtained by substituting each letter with its “spam alphabet” equivalent:

A 4 (four) N |/| (pipe backslash pipe)
B |3 (pipe three) O 0 (zero)
C ( (left-parenthesis) P |0 (pipe zero)
D |) (pipe right-parenthesis) Q (,) (left-parenthesis comma right-parenthesis)
E 3 (three) R |? (pipe question-mark)
F |= (pipe equals) S 5 (five)
G 6 (six) T 7 (seven)
H # (pound) U |_| (pipe underscore pipe)
I | (pipe) V // (backslash forward-slash)
J _| (underscore pipe) W //// (backslash forward-slash backslash forward-slash)
K |< (pipe less-than) X >< (greater-than less-than)
L |_ (pipe underscore) Y -/ (minus forward-slash)
M |//| (pipe backslash forward-slash pipe) Z 2 (two)

In this scheme, any plain text message s corresponds to exactly one spam-encoded message Φ(s). The reverse, however, is not necessarily true: a spam-encoded message may correspond to more than one plain text message.

Given a plain text message s, your goal is to determine the number of unique plain text messages whose spam encoding is Φ(s).

Input

The input test file will contain multiple test cases. Each test case consists of a single line containing a plain text string s containing from 1 to 100 upper-case letters. The end-of-file is denoted by a single line containing the word “end”.

Output

For each test case, print the number of unique plain text messages (including the original message) whose spam encoding is Φ(s). The number of unique plain text messages is guaranteed to be no greater than 1,000,000,000.

Sample Input

BU
UJ
THEQUICKBROWNFOXJUMPEDOVERTHELAZYDOGS
end

Sample Output

6
5
144

Hint

In the first test case, the spam encoding of ‘BU’ is ‘|3|_|’. The 6 plain text messages with this spam encoding are ‘BU’, ‘IEU’, ‘BIJ’, ‘IEIJ’, ‘BLI’, and ‘IELI’. In the second test case, the spam encoding of ‘UJ’ is ‘|_|_|’. The 5 plain text messages with this spam encoding are ‘UJ’, ‘LU’, ‘IJJ’, ‘LLI’, and ‘LIJ’.

Source

Stanford Local 2006
 
 
 
问有多少种不同的解码方式
首先转换成spam
把substr函数用错一直出不了正确结果,调试几个小时。。。
substr(start,n)返回从Start开始的n个字符组成的字符串
状态:
d[i]表示前i个解码方法数
 
状态转移方程:
d[i]+=d[j]   if(str[j...i] is code)
 
边界:
d[0]=1
 
代码:
  #include<iostream> #include<string> #include<cstring> #include<map> using namespace std; map<string,int> q; char str[26][5]={"4","|3","(","|)","3","|=","6","#","|","_|","|<","|_","|///|","|//|","0","|0","(,)","|?","5","7","|_|","///","//////","><","-/","2"}; int main() { q["4"]=1,q["|3"]=2,q["("]=3,q["|)"]=4,q["3"]=5,q["|="]=6,q["6"]=7,q["#"]=8,q["|"]=9,q["_|"]=10,q["|<"]=11,q["|_"]=12,q["|///|"]=13,q["|//|"]=14,q["0"]=15,q["|0"]=16,q["(,)"]=17,q["|?"]=18, q["5"]=19,q["7"]=20,q["|_|"]=21,q["///"]=22,q["//////"]=23,q["><"]=24,q["-/"]=25,q["2"]=26; char s[505]; while(scanf("%s",s),strcmp(s,"end")) { string t,tt; int i,j,d[505]={0}; for(i=0;s[i];i++) t+=str[s[i]-'A']; for(d[0]=i=1;i<=t.length();i++) for(j=1;j<=4&&j<=i;j++) if(q[t.substr(i-j,j)]) d[i]+=d[i-j]; printf("%d/n",d[t.length()]); } }

你可能感兴趣的:(POJ3073Spam题解动态规划DP)