UVa 458 The Decoder(解码器)

 The Decoder 

Write a complete program that will correctly decode a set of characters into a valid message. Your program should read a given file of a simple coded set of characters and print the exact message that the characters contain. The code key for this simple coding is a one for one character substitution based upon a single arithmetic manipulation of the printable portion of the ASCII character set.

Input and Output

For example: with the input file that contains:

1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5
1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
1KLJ'pz'{ol'{yhklthyr'vm'{ol'Kpnp{hs'Lx|pwtlu{'Jvywvyh{pvu5

your program should print the message:

*CDC is the trademark of the Control Data Corporation.
*IBM is a trademark of the International Business Machine Corporation.
*DEC is the trademark of the Digital Equipment Corporation.

Your program should accept all sets of characters that use the same encoding scheme and should print the actual message of each set of characters.

Sample Input

1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5
1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
1KLJ'pz'{ol'{yhklthyr'vm'{ol'Kpnp{hs'Lx|pwtlu{'Jvywvyh{pvu5

Sample Output

*CDC is the trademark of the Control Data Corporation.
*IBM is a trademark of the International Business Machine Corporation.
*DEC is the trademark of the Digital Equipment Corporation.

 The Decoder 

Write a complete program that will correctly decode a set of characters into a valid message. Your program should read a given file of a simple coded set of characters and print the exact message that the characters contain. The code key for this simple coding is a one for one character substitution based upon a single arithmetic manipulation of the printable portion of the ASCII character set.

Input and Output

For example: with the input file that contains:

1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5
1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
1KLJ'pz'{ol'{yhklthyr'vm'{ol'Kpnp{hs'Lx|pwtlu{'Jvywvyh{pvu5

your program should print the message:

*CDC is the trademark of the Control Data Corporation.
*IBM is a trademark of the International Business Machine Corporation.
*DEC is the trademark of the Digital Equipment Corporation.

Your program should accept all sets of characters that use the same encoding scheme and should print the actual message of each set of characters.

Sample Input

1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5
1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
1KLJ'pz'{ol'{yhklthyr'vm'{ol'Kpnp{hs'Lx|pwtlu{'Jvywvyh{pvu5

Sample Output

*CDC is the trademark of the Control Data Corporation.
*IBM is a trademark of the International Business Machine Corporation.
*DEC is the trademark of the Digital Equipment Corporation.


解析:

题目比较简单,即使ASCII减7就可以了。但是要注意字符串的输入,可以用scanf函数,也可以用gets函数,但是使用getchar(),注意‘\n'时候不能进行翻译,应该直接换行。


1.使用scanf函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char str[200];

int main()
{
    int i;

    while(~scanf("%s", str)){
        for(i = 0; i<strlen(str); i++){
            printf("%c", str[i]-7);
        }
        printf("\n");
    }
    return 0;
}


2.使用gets函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char str[200];

int main()
{
    int i;

    while(gets(str)){
        for(i = 0; i<strlen(str); i++){
            str[i] = str[i]-7;
        }
        puts(str);
    }
    return 0;
}


3.使用getchar()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int i;
    char c;
    while((c = getchar())!=EOF){
        if(c!='\n') printf("%c", c-7);
        else printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Transformer的Decoder解码器是Transformer模型中的一个重要组件,用于生成目标序列。它由多个Transformer Block组成,每个Block包含自注意力机制和前馈神经网络。Decoder的输入是编码器的输出和目标序列的嵌入表示。 下面是一个示例代码,演示了如何使用Transformer的Decoder解码器生成目标序列[^1]: ```python import torch import torch.nn as nn class TransformerDecoder(nn.Module): def __init__(self, num_layers, embed_size, num_heads, feed_forward_dim, dropout_rate): super(TransformerDecoder, self).__init__() self.num_layers = num_layers self.decoder_layers = nn.ModuleList([ TransformerDecoderLayer(embed_size, num_heads, feed_forward_dim, dropout_rate) for _ in range(num_layers) ]) def forward(self, enc_output, target_seq): output = target_seq for layer in self.decoder_layers: output = layer(output, enc_output) return output class TransformerDecoderLayer(nn.Module): def __init__(self, embed_size, num_heads, feed_forward_dim, dropout_rate): super(TransformerDecoderLayer, self).__init__() self.self_attention = nn.MultiheadAttention(embed_size, num_heads, dropout=dropout_rate) self.encoder_attention = nn.MultiheadAttention(embed_size, num_heads, dropout=dropout_rate) self.feed_forward = nn.Sequential( nn.Linear(embed_size, feed_forward_dim), nn.ReLU(), nn.Linear(feed_forward_dim, embed_size) ) self.layer_norm1 = nn.LayerNorm(embed_size) self.layer_norm2 = nn.LayerNorm(embed_size) self.layer_norm3 = nn.LayerNorm(embed_size) self.dropout = nn.Dropout(dropout_rate) def forward(self, target_seq, enc_output): # Self-attention self_attention_output, _ = self.self_attention(target_seq, target_seq, target_seq) self_attention_output = self.dropout(self_attention_output) self_attention_output = self.layer_norm1(target_seq + self_attention_output) # Encoder attention encoder_attention_output, _ = self.encoder_attention(self_attention_output, enc_output, enc_output) encoder_attention_output = self.dropout(encoder_attention_output) encoder_attention_output = self.layer_norm2(self_attention_output + encoder_attention_output) # Feed forward feed_forward_output = self.feed_forward(encoder_attention_output) feed_forward_output = self.dropout(feed_forward_output) output = self.layer_norm3(encoder_attention_output + feed_forward_output) return output # 使用示例 num_layers = 6 embed_size = 512 num_heads = 8 feed_forward_dim = 2048 dropout_rate = 0.1 decoder = TransformerDecoder(num_layers, embed_size, num_heads, feed_forward_dim, dropout_rate) enc_output = torch.randn(batch_size, max_token_count, embed_size) target_seq = torch.randn(batch_size, target_seq_length, embed_size) output = decoder(enc_output, target_seq) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值