UVA 213 Message Decoding

题目链接:https://vjudge.net/problem/UVA-213

题目翻译摘自《算法禁赛入门经典》

题目大意

  考虑下面的 01 串序列:
  0, 00, 01, 10, 000, 001, 010, 011, 100, 101, 110, 0000, 0001, …, 1101, 1110, 00000, …
  首先是长度为 1 的串,然后是长度为 2 的串,依此类推。如果看成二进制,相同长度的后一个串等于前一个串加 1。注意上述序列中不存在全为 1 的串。
  你的任务是编写一个解码程序。首先输入一个编码头(例如AB#TANCnrtXc),则上述序列的每个串依次对应编码头的每个字符。例如,0对应A,00对应B,01对应#,…,110对应X,0000对应c。接下来是编码文本(可能由多行组成,你应当把它们拼成一个长长的01串)。编码文本由多个小节组成,每个小节的前3个数字代表小节中每个编码的长度(用二进制表示,例如010代表长度为2),然后是各个字符的编码,以全1结束(例如,编码长度为2的小节以11结束)。编码文本以编码长度为000的小节结束。
  例如,编码头为$#**,编码文本为0100000101101100011100101000,应这样解码:010(编码长度为2)00(#)00(#)10(*)11(小节结束)011(编码长度为3)000()111(小节结束)001(编码长度为1)0($)1(小节结束)000(编码结束)。

分析

  大部分时间都在弄输入输出。。。
 

代码如下

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3  
  4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  6 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  7 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 12  
 13 #define pr(x) cout << #x << " = " << x << "  "
 14 #define prln(x) cout << #x << " = " << x << endl
 15  
 16 #define LOWBIT(x) ((x)&(-x))
 17  
 18 #define ALL(x) x.begin(),x.end()
 19 #define INS(x) inserter(x,x.begin())
 20  
 21 #define ms0(a) memset(a,0,sizeof(a))
 22 #define msI(a) memset(a,inf,sizeof(a))
 23 #define msM(a) memset(a,-1,sizeof(a))
 24 
 25 #define MP make_pair
 26 #define PB push_back
 27 #define ft first
 28 #define sd second
 29  
 30 template<typename T1, typename T2>
 31 istream &operator>>(istream &in, pair<T1, T2> &p) {
 32     in >> p.first >> p.second;
 33     return in;
 34 }
 35  
 36 template<typename T>
 37 istream &operator>>(istream &in, vector<T> &v) {
 38     for (auto &x: v)
 39         in >> x;
 40     return in;
 41 }
 42  
 43 template<typename T1, typename T2>
 44 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 45     out << "[" << p.first << ", " << p.second << "]" << "
";
 46     return out;
 47 }
 48 
 49 inline int gc(){
 50     static const int BUF = 1e7;
 51     static char buf[BUF], *bg = buf + BUF, *ed = bg;
 52     
 53     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
 54     return *bg++;
 55 } 
 56 
 57 inline int ri(){
 58     int x = 0, f = 1, c = gc();
 59     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
 60     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
 61     return x*f;
 62 }
 63 
 64 template<class T>
 65 inline string toString(T x) {
 66     ostringstream sout;
 67     sout << x;
 68     return sout.str();
 69 }
 70  
 71 typedef long long LL;
 72 typedef unsigned long long uLL;
 73 typedef pair< double, double > PDD;
 74 typedef pair< int, int > PII;
 75 typedef pair< int, PII > PIPII;
 76 typedef pair< string, int > PSI;
 77 typedef pair< int, PSI > PIPSI;
 78 typedef set< int > SI;
 79 typedef set< PII > SPII;
 80 typedef vector< int > VI;
 81 typedef vector< VI > VVI;
 82 typedef vector< PII > VPII;
 83 typedef map< int, int > MII;
 84 typedef map< int, PII > MIPII;
 85 typedef map< PII, int > MPIII;
 86 typedef map< string, int > MSI;
 87 typedef map< string, string > MSS;
 88 typedef map< PII, string > MPIIS;
 89 typedef multimap< int, int > MMII;
 90 //typedef unordered_map< int, int > uMII;
 91 typedef pair< LL, LL > PLL;
 92 typedef vector< LL > VL;
 93 typedef vector< VL > VVL;
 94 typedef priority_queue< int > PQIMax;
 95 typedef priority_queue< int, VI, greater< int > > PQIMin;
 96 const double EPS = 1e-6;
 97 const LL inf = 0x7fffffff;
 98 const LL infLL = 0x7fffffffffffffffLL;
 99 const LL mod = 1e9 + 7;
100 const int maxN = 1e4 + 7;
101 const LL ONE = 1;
102 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
103 const LL oddBits = 0x5555555555555555;
104 
105 string str;
106 char dic[8][1 << 8];
107 
108 inline int getInt(int n) {
109     int ret = 0;
110     char tmp;
111     while(n--) {
112         cin >> tmp;
113         if(tmp == '
' || tmp == '
') continue;
114         ret = ret * 2 + tmp - '0';
115     }
116     return ret;
117 }
118 
119 int main(){
120     //freopen("MyOutput.txt","w",stdout);
121     //freopen("input.txt","r",stdin);
122     INIT(); // 有了这个就不能用getchar,printf和scanf等等了 
123     while(getline(cin, str)) {
124         int x = 1, y = 0;
125         Rep(i, str.size()) {
126             if(y > (ONE << x) - 2) {
127                 ++x;
128                 y = 0;
129             }
130             dic[x][y++] = str[i];
131         }
132         
133         int len;
134         while(len = getInt(3)) {
135             while(1) {
136                 int value = getInt(len);
137                 if(!dic[len][value]) break;
138                 cout << dic[len][value];
139             }
140         }
141         cout << endl;
142         getline(cin, str); // 处理掉多余的尾巴 
143     }
144     return 0;
145 }
View Code
原文地址:https://www.cnblogs.com/zaq19970105/p/11024518.html