UVA - 213 Message Decoding (输入字符串并对单个字符进行操作的输入输出)

POINT:

  关于表示一个编码:利用code字符数组表示一个编码字符,其中code[len][val]表示长度为len,二进制值为val的字符;

主程序如下:

 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 0x7fffffff
21 #define maxn 1010
22 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 }

其中readcodes()函数用来读取编号,readint()函数用来读取c位二进制字符并转化为整数(val);

如何处理“编码文本可由多行组成”

 1 bool readcodes()
 2 {
 3     memset(code, 0, sizeof(code));//清空数组
 4     code[1][0] = readchar();//输入开始;单个字符输入
 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 }

readchar()函数代码如下:

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

另外,Printcodes()函数本身对题目没有要求,但是对于调试代码是有用的。

时间: 2024-12-13 11:03:06

UVA - 213 Message Decoding (输入字符串并对单个字符进行操作的输入输出)的相关文章

UVa 213 Message Decoding(World Finals1991,字符串)

 Message Decoding  Some message encoding schemes require that an encoded message be sent in two parts. The first part, called the header, contains the characters of the message. The second part contains a pattern that represents the message. You must

UVa 213 - Message Decoding

将字符用01串进行编码,并把下面的01串转换成对应字符串 打了一遍书上的样例程序.. 读取单个字符的函数来忽略换行符还是很神奇的 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 char code[8][1<<8]; 6 char readchar(){ 7 while(1){ 8 char ch=getchar(); 9 if(c

[UVa 213]Message Decoding,ACM/ICPC World Finals 1991 信息解码

A children's board game consists of a square array of dots that contains lines connecting some of the pairs of adjacent dots. One part of the game requires that the players count the number of squares of certain sizes that are formed by these lines.

UVa 213 Message Decoding (信息编码)

该题目作为放假回归正轨的第一道正式做的题目,被卡了好久,唉,怪我有颗太浪的心 解题思路: 1.把编码头用二维字符数组存储起来,a[x][y]存储对应的字符,x表示编码的长度,y表示编码的大小, 存储时注意y<2^x - 1的 2.由于编码文本可能是由多行组成,所以需要判断和跳过对换行符的处理,为方便处理,将编码文本视作字符处理 #include<iostream> #include<CString> #include<cmath> #include<stdl

uva 213 - Message Decoding (我觉得我的方法要比书上少很多代码,不保证好……)

#include<stdio.h> #include<math.h> #include<string.h> char s[250]; char a[10][250]; int a1[4]; int a2[250]; char ch; int init(int len) { int tt=0; for(int i=1;i<=7;i++) { for(int j=0;j<(int)pow(2,i)-1;j++) { a[i][j]=s[tt++]; if(tt&

41 正则中匹配多个字符串匹配任意单个字符正则中使用字符串重复、可选和特殊字符

第三课 正则中匹配多个字符串 # 匹配多个字符串 # 择一匹配符号, | import re s = 'Python|Ruby|Java|Swift' m = re.match(s,'PythonRuby') print(m) # <re.Match object; span=(0, 6), match='Python'> m = re.search(s, 'I love Python.') print(m) # <re.Match object; span=(7, 13), match

字符数组输入不能带&amp;,单个字符输入带&amp;

代码 #include<stdio.h>main(){ char a[20];char *p; printf("请输入小于20的字符串"); scanf("%s",a);//a是数组名,就是指针,代表首地址. p=a; while(*p!='\0')p++; printf("%d",p-a);} 原文地址:https://www.cnblogs.com/T0807/p/9865500.html

C语言字符型数据(字符)

字符型数据的表示 字符型数据就是字符. 字符型数据是用单引号括起来的一个字符.例如: 'a'.'b'.'='.'+'.'?' 都是合法字符型数据. 在C语言中,字符型数据有以下特点: 字符型数据只能用单引号括起来,不能用双引号或其它括号. 字符型数据只能是单个字符,不能是字符串. 字符可以是字符集中任意字符.但数字被定义为字符型之后就不能参与数值运算.如'5'和5 是不同的.'5'是字符型数据,不能参与运算. 转义字符 转义字符是一种特殊的字符.转义字符以反斜线"\"开头,后跟一个或几

【转载】从键盘输入字符串并输出该字符串(汇编程序)

原文地址http://blog.sina.com.cn/s/blog_4b7bd3380100a76z.html 1 DATAS SEGMENT 2 STR DB"please input a string:$" 3 BUF DB 20 4 DB ? 5 DB 20 DUP (?) 6 CRLF DB 0AH,0DH,"$";此处输入数据段代码 7 DATAS ENDS 8 STACKS SEGMENT STACK 9 DB 200 DUP(?) ;此处输入堆栈段代