# 1.（字符识别）

example：
1 1 1
1 0 0
0 1 0

1 0 0
0 1 0
1 1 1

//[Exam1] Character Recognize
/*
case 1:
2 3 2
1 1 1
0 1 0
1 0 0
1 0 0

case 2:
2 2 5
1 1 1 1
1 1 0 0
1 1 1 0
1 0 0 1
0 0 0 1

case 3:
2 2 4
1 1 1 1
1 1 0 0
1 1 1 0
1 0 0 1

*/
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct node{
int num;
node* left;
node* right;
bool leaf;
};
node* root;
queue<node*> level;
vector<node*> levelV;
node* born;
void destroyTree(node* cur)
{
if(cur==NULL)
return;
else
{
destroyTree(cur->left);
destroyTree(cur->right);
delete cur;
}
}
void creatTree(node* &cur,int value)
{
if(value==0)
{
if(cur->left==NULL)
{
born=new node();
born->leaf=false;
born->left=NULL;
born->right=NULL;
born->num=-1;
cur->left=born;
cur=born;
}
else
cur=cur->left;
}
else
{
if(cur->right==NULL)
{
born=new node();
born->leaf=false;
born->left=NULL;
born->right=NULL;
born->num=-1;
cur->right=born;
cur=born;
}
else
cur=cur->right;
}
}
bool mergeTest(node* a,node* b)//将A合并到B中，即在a的遍历中，在b的相同路径上不能出现叶子节点
{
if(a->leaf&&b->leaf)
return false;
if(a->left&&b->left)
if(!mergeTest(a->left,b->left))
return false;
if(a->right&&b->right)
if(!mergeTest(a->right,b->right))
return false;
return true;
}
void merge(node* a,node* b)//将a合并到b中
{
if(a->left!=NULL)
{
if(b->left==NULL)
b->left=a->left;
else
merge(a->left,b->left);
}
if(a->right!=NULL)
{
if(b->right==NULL)
b->right=a->right;
else
merge(a->right,b->right);
}
}
bool filter(vector<node*>& tmp)
{
int len=tmp.size();
//cout<<"try delete size: "<<len<<endl;
int i;
for(i=0;i<len;++i)
{
//cout<<"round: "<<i<<endl;
if(tmp[i]->left==NULL)
continue;
else if(tmp[i]->right==NULL)
continue;
else if(!mergeTest(tmp[i]->left,tmp[i]->right))
break;
}
//cout<<i<<" "<<len<<endl;
node* cur;
if(i>=len)
{
for(i=0;i<len;++i)
{
if(tmp[i]->left==NULL)
{
cur=tmp[i]->right;
}
else if(tmp[i]->right==NULL)
{
cur=tmp[i]->left;
}
else
{
merge(tmp[i]->left,tmp[i]->right);
cur=tmp[i]->right;
}
tmp[i]->left=cur->left;
tmp[i]->right=cur->right;
tmp[i]->leaf=cur->leaf;
//cout<<"deleted num: "<<cur->num<<endl;
delete cur;
}
return true;
}
return false;
}
int spare()
{
node* flag=NULL;
while(!level.empty())level.pop();
levelV.clear();
level.push(root);
level.push(flag);
node* cur;
int countCanDel=0;
while(!level.empty())
{
cur=level.front();
level.pop();
if(cur!=NULL)
{
if(cur->leaf)
break;
levelV.push_back(cur);
}
else
{
if(levelV.size()==0)
break;
if(filter(levelV))//删除成功
{
//cout<<"deleteing"<<endl;
++countCanDel;
for(int i=0;i<levelV.size();++i)
{
level.push(levelV[i]);
//  cout<<levelV[i]->num<<" ";
}
//cout<<endl;
level.push(flag);
}
else
{
//cout<<"next "<<endl;
for(int i=0;i<levelV.size();++i)
{
if(levelV[i]->left!=NULL)
level.push(levelV[i]->left);
if(levelV[i]->right!=NULL)
level.push(levelV[i]->right);
}
level.push(flag);
}
levelV.clear();
}
}
return countCanDel;
}
int main()
{
root=new node();
root->leaf=false;
root->left=NULL;
root->right=NULL;
root->num=0;
int n,m,p;
cin>>n>>m>>p;
int i,j,k;
int value;
node* cur;
//int start=1;
for(i=0;i<p;++i)
{
cur=root;
for(j=0;j<n;++j)
for(k=0;k<m;++k)
{
cin>>value;
creatTree(cur,value);
//cout<<"orignal: "<<cur->num<<endl;
//cur->num=start++;
//cout<<"after: "<<cur->num<<endl;
}
cur->leaf=true;
}
int cost=spare();
cout<<"mid: "<<cost<<endl;
cout<<"final: "<<n*m-cost;
return 0;
}

# 2 .输出最大值

/*
3
1 2 3
*/
#include <iostream>
#include <vector>
using namespace std;
unsigned int sum=0;
vector<unsigned int> value;
int main() {
int num;
cin>>num;
value.resize(num+1);
for(int i=0;i<num;++i)
{
cin>>value[i];
sum=sum^value[i];
}
//cout<<sum<<endl;
unsigned int maxSum=sum,tmpSum=sum;
unsigned int midSum;
int start,end;
for(int i=0;i<num;++i)
{
tmpSum=tmpSum^value[i];
//cout<<"no: "<<i<<" "<<tmpSum<<endl;
if(tmpSum>maxSum)
{
//cout<<"ac"<<endl;
maxSum=tmpSum;
}
else if(tmpSum<sum)
{
//cout<<"fail"<<endl;
tmpSum=sum;
}
}
cout<<maxSum<<endl;
return 0;
}

11-16 5046

10-12 2742

03-08 636

#### 百度初级认证考试试题

2017年08月26日 3.88MB 下载

08-03 3659

04-28 982

09-12 612

07-27 1976

10-19 508

06-06 2235