首页 > 代码库 > UVa 213,World Finals 1991,信息解码

UVa 213,World Finals 1991,信息解码

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=149

紫书P83

技术分享

解题报告:

思路很巧。每个字符这样记录,由于同一个值可能有好几种对应,比如0,00,000,这样用一个二维数组code[len][v] 记录字符。

二进制再熟悉一遍。扫一遍长度为len的二进制所有数值。for(int v = 0;v<(1<<len)-1;v++)

#include <stdio.h>#include <string.h>int code[8][1<<8];int readchar(){    for(;;)    {        int ch = getchar();        if(ch != \n && ch != \r) return ch;    }}int readint(int c) {  int v = 0;  while(c--) v = v * 2 + readchar() - 0;  return v;}int readcode(){    memset(code,0,sizeof(code));    code[1][0] = readchar();    for(int i = 2; i<=7; i++)    {        for(int j=0; j<(1<<i)-1; j++)        {            int ch = getchar();            if(ch==EOF) return 0;            if(ch==\n||ch==\r) return 1;            code[i][j] = ch;        }    }    return 1;}void printcodes(){    for(int len = 1; len <= 3; len++)        for(int i = 0; i < (1<<len)-1; i++)        {            if(code[len][i] == 0) return;            printf("code[%d][%d] = %c\n", len, i, code[len][i]);        }}int main(){    //freopen("input.txt","r",stdin);    while(readcode())    {        while(true)        {            int len = readint(3);            if(len == 0) break;            while(true)            {                int v = readint(len);                if(v == (1 << len)-1) break;                putchar(code[len][v]);            }        }        putchar(\n);    }    return 0;}

 

UVa 213,World Finals 1991,信息解码