这篇文章是之前写的,我也忘了是什么时间写的了,今天看自己的内容管理发现这篇博客还没有发出去,当时可能是忘记发了
这是一套全英文的题目。
A . Phone Number--题目比较简单
Description
We know that if a phone number A is another phone number B’s prefix, B is not able to be called. For an example, A is 123 while B is 12345, after pressing 123, we call A, and not able to call B.
Given N phone numbers, your task is to find whether there exits twonumbers A and B that A is B’s prefix.
Input
The input consists of several test cases.
The first line of input in each test case contains one integer N (0<N<1001), represent the number of phone numbers.
The next line contains N integers, describing the phone numbers.
The last case is followed by a line containing one zero.
Output
For each test case, if there exits a phone number that cannot be called, print “NO”, otherwise print “YES” instead.
样例
Input 复制
2
012
012345
2
12
012345
0
Output
NO
YES
题目大意: 多组输入!每一组第一行是号码数,接下来n行每行一个电话号码。我们需要判断一下每组其中的每一个电话号码是不是另一个的前缀,不允许出现这种情况,输出“NO”,否则的话允许。
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll N = 1e3+10;
vector<string>v;//vector来存电话号码
int main()
{
int n;
while(scanf("%d",&n)!=EOF){//多组输入
if(n == 0){//结束条件
return 0;
}
v.clear();//清空vector数组
for(int i=0;i<n;i++){
string a;
cin>>a;
v.push_back(a);//将这个电话号码插入
}
int flag=0;
sort(v.begin(),v.end());//按照字典序排一下顺序
for(int i=0;i<n-1;i++){
// cout<<v[i+1].find(v[i])<<endl;
if(v[i+1].find(v[i])==0){
//使用find函数,找这个的位置,如果是在开头位置,这不符合条件
flag=1;
}
}
if(flag)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
return 0;
}
B . Balloons
Description
Both Saya and Kudo like balloons. One day, they heard that in the central park, there will be thousands of people fly balloons to pattern a big image.
They were very interested about this event, and also curious about the image.
Since there are too many balloons, it is very hard for them to compute anything they need. Can you help them?
You can assume that the image is an N∗Nmatrix, while each element can be either balloons or blank.
Suppose element A and element B are both balloons. They are connected if:
- They are adjacent;
- There is a list of element C1,C2,…,Cn, while A and C1 are connected, C1 and C2 are connected …Cn and B are connected.
And a connected block means that every pair of elements in the block is connected, while any element in the block is not connected with any element out of the block.
To Saya, element A(xa,ya) and B(xb,yb) is adjacent if |xa−xb|+|ya−yb|≤1.
But to Kudo, element A(xa,ya)and element B(xb,yb) is adjacent if |xa−xb|≤1 and |ya−yb|≤1
They want to know that there’s how many connected blocks with there own definition of adjacent?
Input
The input consists of several test cases.
The first line of input in each test case contains one integer N (0<N≤100), which represents the size of the matrix.
Each of the next N lines contains a string whose length is N, represents the elements of the matrix. The string only consists of 0 and 1, while 0 represents a block and 1 represents balloons.
The last case is followed by a line containing one zero.
Output
For each case, print the case number (1, 2 …) and the connected block’s numbers with Saya and Kudo’s definition. Your output format should imitate the sample output. Print a blank line after each test case.
样例
Input
5
11001
00100
11111
11010
100100
Output
Case 1: 3 2
题目大意:寻找连接在一起的气球,有两种标准。第一种:两个气球的坐标需要满足|xa−xb|+|ya−yb|≤1;第二种:两个气球的坐标需要满足 |xa−xb|≤1 and |ya−yb|≤1 。即:第一种是周围上下左右4个,第二种是周围的8个。最后需要输出一共可以连成几块。
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[110][110];
int vis[110][110];//标记是否已经连上了
void bj1(int x,int y){//第一种的标记,标记符合条件的格子
if(a[x][y]==0||vis[x][y]==1){//这个格子没有气球或者这个气球已经被连接上了
return;
}
vis[x][y]=1;//将这个气球格子标记
//分别对4个位置进行标记
bj1(x+1,y);
bj1(x,y+1);
bj1(x-1,y);
bj1(x,y-1);
}
void bj2(int x,int y){//第二种的标记,标记符合条件的格子
if(a[x][y]==0||vis[x][y]==1){//这个格子没有气球或者这个气球已经被连接上了
return ;
}
vis[x][y]=1;//将这个气球格子标记
//分别对周围的8个位置进行标记
bj2(x-1,y-1);
bj2(x-1,y);
bj2(x-1,y+1);
bj2(x,y-1);
bj2(x,y+1);
bj2(x+1,y-1);
bj2(x+1,y);
bj2(x+1,y+1);
}
int main()
{
int n;
int cnt=0;
while(scanf("%d",&n)!=EOF){
if(n==0){//结束标记
return 0;
}
cnt++;//记录多组输入的第几组
memset(a,0,sizeof(a));//初始化,如果不初始化会出问题,这卡了一会
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++){//第i行
string s;
cin>>s;//第i行的标志
for(int j=1;j<=n;j++){//将这一行的标志拆开存到数组中
a[i][j]=s[j-1]-'0';
}
}
//第一种情况的循环跑
int cnt1=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(!vis[i][j]&&a[i][j]){//判断这个格子有没有已经别标记或者没有气球
bj1(i,j);
cnt1++;//连接一块区域
}
}
}
//第二种情况的循环
int cnt2=0;
memset(vis,0,sizeof(vis));
//初始化,很重要,这理如果没有初始化会沿用第一种情况标记的,导致结果出现很大错误
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(!vis[i][j]&&a[i][j]){
bj2(i,j);
cnt2++;
}
}
}
printf("Case %d: %d %d\n\n",cnt,cnt1,cnt2);//输出
}
}