Message decoding (input string and input and output for operations on a single character)

Source: Internet
Author: User

 

Point:

About encoding: A Code character array is used to represent an encoding character. Code [Len] [Val] indicates a character whose length is Len and whose binary value is Val;

 

The main program is as follows:

 1 #include <iostream> 2 #include <sstream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <cctype>10 #include <algorithm>11 #include <cmath>12 #include <deque>13 #include <queue>14 #include <map>15 #include <stack>16 #include <list>17 #include <iomanip>18 19 using namespace std;20 #define INF 0x7fffffff21 #define maxn 101022 typedef unsigned long long ull;23 24 int code[8][1<<8];25 26 int main()27 {28     while(readcodes())29     {30         //printcodes();31         for(;;)32         {33             int len = readint(3);34             if(len == 0)    break;35             //printf("len = %d\n", len);36 37             for(;;)38             {39                 int v = readint(len);40                 //printf("v = %d\n", v);41                 if(v == (1<<len)-1) break;42                 putchar(code[len][v]);43             }44         }45         putchar(‘\n‘);46     }47     return 0;48 }

 

The readcodes () function is used to read numbers, and the readint () function is used to read c-bit binary characters and convert them into integers (VAL );

How to handle "encoding text may consist of multiple lines"

1 bool readcodes () 2 {3 memset (Code, 0, sizeof (CODE); // clear the Array 4 Code [1] [0] = readchar (); // start with input; enter 5 for (INT Len = 2; Len <= 7; Len ++) 6 {7 for (INT val = 0; val <(1 <Len)-1; Val ++) 8 {9 int CH = getchar (); 10 if (CH = EOF) return 0; 11 if (CH = '\ n' | CH =' \ R') return 1; 12 13 code [Len] [Val] = CH; 14} 15} 16 return 1; 17} 18 int readint (INT c) 19 {20 int V = 0; 21 While (c --) 22 V = V * 2 + readchar ()-'0'; 23 return V; 24}

The readchar () function code is as follows:

1 int readchar()2 {3     for(;;)4     {5         int ch = getchar();6         if(ch != ‘\n‘ && ch != ‘\t‘)    return ch;7     }8 }

 

In addition, the printcodes () function itself has no requirements for the question, but it is useful for debugging code.

Message decoding (input string and input and output for operations on a single character)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.