2018-08-11 训练6

  • B  -- The Bits

  • Description

Rudolf is on his way to the castle. Before getting into the castle, the security staff asked him a question:

Given two binary numbers aa and bb of length nn. How many different ways of swapping two digits in aa (only in aa, not bb) so that bitwise OR of these two numbers will be changed? In other words, let cc be the bitwise OR of aa and bb, you need to find the number of ways of swapping two bits in aa so that bitwise OR will not be equal to cc.

Note that binary numbers can contain leading zeros so that length of each number is exactly nn.

Bitwise OR is a binary operation. A result is a binary number which contains a one in each digit if there is a one in at least one of the two numbers. For example, 01010201010_2 OR 10011210011_2 = 11011211011_2.

Well, to your surprise, you are not Rudolf, and you don't need to help him…… You are the security staff! Please find the number of ways of swapping two bits in aa so that bitwise OR will be changed

  • Input

The first line contains one integer nn (2≤n≤1052≤n≤105) — the number of bits in each number.

The second line contains a binary number aa of length nn.

The third line contains a binary number bb of length nn

  • Output

Print the number of ways to swap two bits in aa so that bitwise OR will be changed

  • Sample Input

5
01011
11001

  • Sample Output

4

  • 题目理解

(s_1s_2)=(00)时只有当且仅当(s_1s_2)^{'}=(00)不产生变化,其他条件下也是如此;所以我们只需要对(s_1s_2)=(00)(s_1s_2)=(01)(s_1s_2)=(10)(s_1s_2)=(11)进行统计然后分别与其他配对就能够得到变化的方法数

#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=1e5 + 7;
char s1[maxn],s2[maxn];
int main() {
    int n;
    scanf("%d%s%s",&n,s1,s2);
    int ans=0;
    ll cnt1=0,cnt2=0,cnt3=0,cnt4=0;//注意开 ll
    for (int i=0;i<n;i++) {
        if (s1[i]=='0'&&s2[i]=='0')
            cnt1++;
        else if (s1[i] == '1'&&s2[i] == '0')
            cnt2++;
        else if (s1[i] == '1'&&s2[i] == '1')
            cnt3++;
        else if (s1[i] == '0'&&s2[i] == '1')
            cnt4++;
    }
    printf("%lld\n",cnt1*cnt2+cnt3*cnt1+cnt2*cnt4);
    return 0;
}

 

  • C  -- Immediate Decodability

  • Description

An encoding of a set of symbols is said to be immediately decodable if no code for one symbol is the prefix of a code for another symbol. We will assume for this problem that all codes are in binary, that no two codes within a set of codes are the same, that each code has at least one bit and no more than ten bits, and that each set has at least two codes and no more than eight.
Examples: Assume an alphabet that has symbols {A, B, C, D}
The following code is immediately decodable:
A:01 B:10 C:0010 D:0000
but this one is not:
A:01 B:10 C:010 D:0000 (Note that A is a prefix of C)

  • Input

Write a program that accepts as input a series of groups of records from input. Each record in a group contains a collection of zeroes and ones representing a binary code for a different symbol. Each group is followed by a single separator record containing a single 9; the separator records are not part of the group. Each group is independent of other groups; the codes in one group are not related to codes in any other group (that is, each group is to be processed independently)

  • Output

For each group, your program should determine whether the codes in that group are immediately decodable, and should print a single output line giving the group number and stating whether the group is, or is not, immediately decodable

  • Sample Input

01

10

0010

0000

9

01

10

010

0000

9

  • Sample Output

Set 1 is immediately decodable

Set 2 is not immediately decodable

  • 题目理解

01字典树的应用,对于每个串进行插入操作前查询一下是否存在前缀的现象。如果存在前缀现象有两种可能

  1. 长串先插入
  2. 短串先插入

对于1来说如果在查询的时候没有遇到NULL标记,则自然是情况1;对于2来说需要标记字符串尾当遇到NULL标记的时候如果是字符串尾那么自然是情况2.

在做题的过程中对于字符串9的处理,遇到9输出结果然后销毁已经建立的树.注意对9的操作和01字符串的操作一定要添加if-else否则后面的情况两次插入9,虽然无意义但会导致错误的结果.

//高位值0^0为0所以可以将输入的数标准化为31位01串在搜索的时候贪心搜索不同值
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=2;
char s[15];
bool flag=true;
struct node{
   int ed;
   node* next[maxn];//指向新的内存空间
};
node root;
//刷新一个单词的存储结果
void build_tree(){
   node* p=&root;
   int len=strlen(s);
   for(int i=0;i<len;++i){
     int index=s[i]-'0';
     if(p->next[index]==NULL){
        node* q=(node*)malloc(sizeof(root));
        for(int i=0;i<maxn;++i)
            q->next[i]=NULL;
        //q->k=i;//记录当这一位不相同时的指数值
        q->ed=0;//初始化
        p->next[index]=q;
        p=q;
     }else{
        p=p->next[index];
     }//p指向当前节点
   }p->ed=1;
}
int query(){
    node* p=&root;
    int len=strlen(s);
    for(int i=0;i<len;++i){
        int index=s[i]-'0';
        if(p->next[index]==NULL){
            if(p->ed) return 1;//遍历到前缀
            return 0;//合法
        }
        p=p->next[index];
        if(p->ed) return 1;
    }
    return 1;//前面完全重合
}
void rfree(node* root){
    for(int i=0;i<maxn;++i){
        if(root->next[i]!=NULL)
            rfree(root->next[i]);
    }
    free(root);
    return ;
}
void reset(){
    for(int i=0;i<maxn;++i){
        if(root.next[i]!=NULL)
           rfree(root.next[i]);
        root.next[i]=NULL;//曾经记录过内存但是那个地方释放了记得要修改记录值否则WA
        root.ed=0;
    }
    return ;
}

int main()
{
    /*int a,b;
    a=0,b=1<<30;
    int ans=a^b;
    while(ans){
        printf("%d",ans&1);
        ans=ans>>1;
    }*/
    //int 有符号32位
    int cas=0;
    while(~scanf("%s",s)){
        if(s[0]=='9'){
            if(flag)
              printf("Set %d is immediately decodable\n",++cas);
            else
              printf("Set %d is not immediately decodable\n",++cas);
            reset();
            flag=true;
        }else{
            if(query()) flag=false;
            build_tree();
        }

    }
    return 0;
}

 

  • F  -- Radio Station

  • Description

As the guys fried the radio station facilities, the school principal gave them tasks as a punishment. Dustin's task was to add comments to nginx configuration for school's website. The school has n servers. Each server has a name and an ip (names aren't necessarily unique, but ips are). Dustin knows the ip and name of each server. For simplicity, we'll assume that an nginx command is of form "command ip;" where command is a string consisting of English lowercase letter only, and ip is the ip of one of school servers.

Each ip is of form "a.b.c.d" where a, b, c and d are non-negative integers less than or equal to 255 (with no leading zeros). The nginx configuration file Dustin has to add comments to has m commands. Nobody ever memorizes the ips of servers, so to understand the configuration better, Dustin has to comment the name of server that the ip belongs to at the end of each line (after each command). More formally, if a line is "command ip;" Dustin has to replace it with "command ip; #name" where name is the name of the server with ip equal to ip.

Dustin doesn't know anything about nginx, so he panicked again and his friends asked you to do his task for him.

  • Input

The first line of input contains two integers n and m (1 ≤ n, m ≤ 1000).

The next n lines contain the names and ips of the servers. Each line contains a string name, name of the server and a string ip, ip of the server, separated by space (1 ≤ |name| ≤ 10, name only consists of English lowercase letters). It is guaranteed that all ip are distinct.

The next m lines contain the commands in the configuration file. Each line is of form "command ip;" (1 ≤ |command| ≤ 10, command only consists of English lowercase letters). It is guaranteed that ip belongs to one of the n school servers.

  • Output

Print m lines, the commands in the configuration file after Dustin did his task.

  • Sample Input

2 2
main 192.168.0.2
replica 192.168.0.1
block 192.168.0.1;
proxy 192.168.0.2;

  • Sample Output

block 192.168.0.1; #replica
proxy 192.168.0.2; #main

  • 题目理解

读懂题意就能做的题直接用map就可以,只需要注意那个输入的时候多了一个分号(;)

#include<iostream>
#include<map>
using namespace std;
map<string,string>mp;
int main(){
	int n,m;
	string str,id;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		cin>>str>>id;
		mp[id]=str;
	}
	for(int i=0;i<m;i++){
		string str,id,tmp;
		cin>>str>>id;
        tmp=id.substr(0,id.length()-1);//消除分号
        cout<<str<<" "<<id<<" #"<<mp[tmp]<<endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的基于Transformer算法的代码示例,可以用来预测SNumber。 ```python import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split from tensorflow.keras.models import Model from tensorflow.keras.layers import Input, Dense, Dropout, LayerNormalization, MultiHeadAttention, TimeDistributed, concatenate # 加载数据 data = pd.read_csv('data.csv') X = data.drop(['SNumber'], axis='columns') y = data['SNumber'] # 标准化输入特征 scaler = StandardScaler() X = scaler.fit_transform(X) # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False) # 构建Transformer模型 def transformer_model(input_shape): inputs = Input(shape=input_shape) x = Dense(64, activation='relu')(inputs) x = LayerNormalization()(x) x = Dropout(0.2)(x) x = Dense(32, activation='relu')(x) x = LayerNormalization()(x) x = Dropout(0.2)(x) x = Dense(1)(x) model = Model(inputs=inputs, outputs=x) return model # 训练模型 model = transformer_model((X_train.shape[1],)) model.compile(optimizer='adam', loss='mean_squared_error') model.fit(X_train, y_train, epochs=100, batch_size=16, validation_data=(X_test, y_test)) # 预测结果 y_pred = model.predict(X_test) # 可视化预测结果 plt.plot(y_test.values, label='True') plt.plot(y_pred, label='Predicted') plt.legend() plt.show() ``` 需要注意的是,这只是一个简单的示例,实际上可能需要进行更多的特征工程和调整模型参数来获得更好的预测结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值