信息解码(Message Decoding)ACM题目

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq742762377/article/details/80778136

分析

长度为1的串,可以放1个编码

长度为2的串,可以放3个编码

长度为3的串,可以放7个编码

.....

长度为n的串,可以放 n^2 -1 个编码

思路

1. 用 1<<n  表示 2^n

2.

读取编码头:

codes[][]存储编码头;

Len表示  长度为Len的串

value表示  长度为Len的串的第value个值

例如:长度为 3 的第 2 个编码

先一次输入多个字符,然后用循环调用getchar函数,使其到缓冲区逐一读取字符,并按规律放到codes[Len][value]里面,直到读取到换行符或EOF结束.


3.

读取二进制密文:

①先用循环读取 3位二进制数的 编码长度 (Len),并从二进制转换成十进制  (Len为0的时候结束循环),

②然后往后每次读取Len位二进制数,并把Len位二进制数值转换成十进制value

③然后用 Lenvaluecodes[][] 里面找到对应的编码

④直到读取到 Len位二进制数全为1的时候,即 value = (1<<Len) -1 的时候 结束,跳到 ①



多行读取思路:

for(;;)循环调用getchar 读取字符,当遇到\n \t的时候,就跳过,然后往下读取,当读取到非换行符的时候,就跳出循环



#include<iostream>
using namespace std;

char codes[10][1 << 10 - 1];   // 即 codes[10][ 2^10 -1]

//读取字符
int readchar() { 
    for (;;) {
        int ch = getchar();
        if (ch != '\n'&&ch != '\t') //如果出现回车或换行 ,则跳过回车或换行,往后读取一个字符  (实现多行读取)
            return ch;
    }
}

//读取密文
int readint(int n) {
    int i, v = 0;

    for (i = n - 1; i >= 0; i--)
        v += (readchar() - '0') * (1 << i); //转换成10进制
    return v;
}

//读取编码头 
int readcode() {
    int len, i;
    memset(codes, 0, sizeof(codes));

    for (len = 1; len <= 9; len++) {
        for (i = 0; i < (1 << len) - 1; i++) {
            char ch = getchar();
            if (ch == EOF)return 0; 
            if (ch == '\n' || ch == '\t')return 1; //遇到换行则读取结束
            codes[len][i] = ch;
        }
    }
    return 1; // 超过数codes组范围则读取结束
}


int main() {
    int Len;
    int p = 0, k = 0;
    int value = 0;

    readcode();
    while (1) {
        Len = 0; //编码长度

        Len = readint(3);
        if (Len == 0)break;

        while (1) {
            value = 0;

            value = readint(Len); //读取Len位二进制数值,并转换成十进制

            if (value == (1 << Len) - 1)break; //如果编码全为1,则跳出循环

            putchar(codes[Len][value]);
        }
        putchar('\n');
    }
    return 0;
}


展开阅读全文

acm题目

01-18

poj 1007rnLanguage:DefaultDNA SortingrnTime Limit: 1000MS Memory Limit: 10000K rnTotal Submissions: 61135 Accepted: 24156 rnrnDescriptionrnrnOne measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG'' has only one inversion (E and D)---it is nearly sorted---while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as can be---exactly the reverse of sorted). rnrnYou are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the same length. rnrnInputrnrnThe first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (0 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.rnOutputrnrnOutput the list of input strings, arranged from ``most sorted'' to ``least sorted''. Since two strings can be equally sorted, then output them according to the orginal order.rnSample Inputrnrn10 6rnAACATGAAGGrnTTTTGGCCAArnTTTGGCCAAArnGATCAGATTTrnCCCGGGGGGArnATCGATGCATrnSample OutputrnrnCCCGGGGGGArnAACATGAAGGrnGATCAGATTTrnATCGATGCATrnTTTTGGCCAArnTTTGGCCAAArnSourcern rnrn 题目意思是说有一些字符串,这些字符串的字符不都是按先后顺序排的。这些字符串都对应一个值,这个值是倒序数的总和。例如:AACEDGG只有E和D是反的,所以这个值是1。GCA这个串的值就是3(GA、GC、CA都是反的)。rn#include rn#include rnint Sum(char a[],int n)rnrn int k,j,sum=0;rn for (j=0;ja[k]) sum++; rn rnrn return sum;rnrnint main(int argc, char *argv[])rnrn int n,m;rn int i,j,t;rn char a[50][110],c[110];rn int b[50];rn scanf("%d%d",&n,&m);rn for (i=0;ib[j+1])rn rn t=b[j]; strcpy(c,a[j]);rn b[j]=b[j+1]; strcpy(a[j],a[j+1]); rn b[j+1]=t; strcpy(a[j+1],c); //若前个b[i]大的话就交换 连同字符窜一起交换 rn rn rn for (i=0;i 论坛

ACM解码问题

06-03

调用ACM里面一些接口函数解音频数据,最后函数返回值都是对的,但解出来的数据不对。不知道是哪里出问题了。rn WAVEFORMATEX destWaveFormat;rn memcpy(&destWaveFormat,&waveformat,sizeof(WAVEFORMATEX));//waveformat是读取AVI或许到的音频格式相关数据rn destWaveFormat.wFormatTag = WAVE_FORMAT_PCM;rn destWaveFormat.wBitsPerSample = 16;rn destWaveFormat.nBlockAlign = destWaveFormat.wBitsPerSample*destWaveFormat.nChannels/8;rn destWaveFormat.nAvgBytesPerSec = destWaveFormat.nBlockAlign*destWaveFormat.nSamplesPerSec;rn HACMSTREAM hstr = NULL;rn //打开转换流rn MMRESULT mmr = acmStreamOpen(&hstr,NULL, pWaveFormat, &destWaveFormat,NULL, 0,0,ACM_STREAMOPENF_NONREALTIME); rnrn DWORD dwDstBytes = bufferSize*destWaveFormat.wBitsPerSample/destWaveFormat.wBitsPerSample;//bufferSize是读取AVI文件里获取到的建议缓冲区大小rn dwDstBytes = dwDstBytes*3/2;rn BYTE *pDstData = new BYTE[dwDstBytes];rn BYTE *pSrcData = new BYTE[bufferSize];rnrn //准备转换流的信息头rn ACMSTREAMHEADER acmStreamHeader;rn memset (&acmStreamHeader, 0, sizeof(acmStreamHeader)); //初始化rnrn acmStreamHeader.cbStruct = sizeof(acmStreamHeader);rn acmStreamHeader.pbSrc = pSrcData;//源音频数据区rn acmStreamHeader.cbSrcLength = bufferSize;rn acmStreamHeader.pbDst = pDstData;//压缩后音频数据缓冲区 rn acmStreamHeader.cbDstLength = dwDstBytes;rnrn mmr = acmStreamPrepareHeader(hstr,&acmStreamHeader,0);rnrn mmr = acmStreamConvert(hstr,&acmStreamHeader, 0);rnrn调试的时候,mmr的返回值都是0,就说明调用成功了。但是acmStreamHeader里面保存的解码后的数据不对,是乱码。rn 论坛

没有更多推荐了,返回首页