- 问题描述
There's no end to revenge.
Pigs like to eat birds' eggs, so birds decide to revenge.
For this, pigs decide to revenge as well.
Pigs give a task to a birdlike pig. They let he sneak into the birds group so that they can destroy the birds group.
Every bird has a ID number (It may not be unique). To let pigs know, the birdlike pig makes the "AND operation" amount all the birds' ID number and sign himself to the answer.For example: Birds are signed as 5, 3, 4. So the birdlike pig signs himself as (5 & 3 & 4 = 0).
One day, a group birds pass by pigs' home. So the angry pig want to know whether the birdlike pig is in.- 输入
- This problem has several cases. The first line of each case is an integer N (2 <= N <= 100 000).
Then follows a line with N integers, indicates the ID of each bird/birdlike pig. - 输出
- For each case, if you can find the birdlike pig then output the ID of birdlike pig. Or output 'CAUTION: NO BIRDLIKE'.构造一个数Ai=bird[1]&bird[2]&.....&bird[i-1]&bird[i+1]&......bird[n],与乘积分析类似,将Ai分为2部分,分别用数组C,D表示则C[i]=C[i-1]&bird[i],D[i]=D[i+1]&bird[i+1]
#include<stdio.h>
#include<string.h>
int bird[100010];
int first[100010];
int last[100010];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&bird[i]);
if(i==1)
first[1]=bird[1];
else
first[i]=first[i-1]&bird[i];
}
for(int i=n;i>=1;i--)
if(i==n)
last[n]=bird[n];
else
last[i]=(last[i+1]&bird[i]);
bool p=false;
for(int i=2;i<=n-1;i++)
{
if((first[i-1]&last[i+1])==bird[i])
{
printf("%d\n",bird[i]);
p=true;
break;
}
}
if(p)
continue;
else
{
if(bird[1] == last[2])
printf("%d\n",bird[1] );
else if(bird[n] == first[n-1])
printf("%d\n",bird[n] );
else
printf("CAUTION: NO BIRDLIKE\n");
}
}
return 0;
}