Posted on 2009-06-23 16:39
Hero 阅读(120)
评论(0) 编辑 收藏 引用 所属分类:
代码如诗--ACM
1 #include <iostream>
2 #include <vector>
3 #include <string>
4 #include <algorithm>
5 using namespace std ;
6
7 class BinaryCode
8 {
9 public:
10 vector<string> decode( string message )
11 {
12 vector<string> revec ;
13 revec.clear() ;
14
15 int num[100] = {0} ;
16 for( int i=0; i<message.length(); i++ )
17 {
18 num[i+1] = message[i] - '0' ;
19 }
20
21 int ans[100] = {0} ;
22 //case 1 :
23 ans[1] = 0 ; bool none = false ;
24 for( int i=2; i<=message.length(); i++ )
25 {
26 ans[i] = num[i-1] - ans[i-1] - ans[i-2] ;
27 if( ans[i] < 0 || ans[i] > 1 ) none = true ;
28 }
29
30 int final = message.length() ;
31 if( num[final] != ans[final-1] + ans[final] || none )
32 {
33 //revec[0] = "NONE" ;
34 revec.push_back( "NONE" ) ;
35 }
36 else
37 {
38 string str ; str.clear() ;
39 for( int i=1; i<=message.length(); i++ )
40 {
41 str += (ans[i]+'0') ;
42 }
43 revec.push_back( str ) ;
44 }
45
46 //case 2 :
47 ans[1] = 1 ; none = false ;
48 for( int i=2; i<=message.length(); i++ )
49 {
50 ans[i] = num[i-1] - ans[i-1] - ans[i-2] ;
51 if( ans[i] < 0 || ans[i] > 1 ) none = true ;
52 }
53
54 final = message.length() ;
55 if( num[final] != ans[final-1] + ans[final] || none )
56 {
57 revec.push_back( "NONE" ) ;
58 }
59 else
60 {
61 string str ; str.clear() ;
62 for( int i=1; i<=message.length(); i++ )
63 {
64 str += (ans[i]+'0') ;
65 }
66 revec.push_back( str ) ;
67 }
68
69 return revec ;
70 }
71 };
72
73 int main()
74 {
75 string inn ;
76 while( cin >> inn )
77 {
78 BinaryCode *bin = new BinaryCode() ;
79
80 vector<string> vec = bin->decode( inn ) ;
81
82 cout << vec[0] << endl ;
83 cout << vec[1] << endl ;
84 }
85 return 0 ;
86 }