-
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, OR
=
.
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
- 题目理解
当时只有当且仅当
不产生变化,其他条件下也是如此;所以我们只需要对
、
、
、
进行统计然后分别与其他配对就能够得到变化的方法数
#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来说如果在查询的时候没有遇到NULL标记,则自然是情况1;对于2来说需要标记字符串尾当遇到NULL标记的时候如果是字符串尾那么自然是情况2.
在做题的过程中对于字符串9的处理,遇到9输出结果然后销毁已经建立的树.注意对9的操作和01字符串的操作一定要添加否则后面的情况两次插入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
- 题目理解
读懂题意就能做的题直接用就可以,只需要注意那个输入的时候多了一个分号(;)
#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;
}