1928: Phone Number
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 70 Solved: 28
[ Submit][ Status][ Web Board]
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 two numbers 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.
Sample Input
2
012
012345
2
12
012345
0
Sample Output
NO
YES
HINT
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char a[1005][1005];
int main()
{
int n;
while(cin>>n&&n)
{
for(int i=0;i<n;i++)
cin>>a[i];
int flag=1;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
int len=min(strlen(a[i]),strlen(a[j]));
flag=1;//每次比较都注意
for(int k=0;k<len;k++)
{
if(a[i][k]!=a[j][k])
{
flag=0;
break;
}
}
if(flag) break;
}
if(flag) break;
}
if(!flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
1929: Balloons
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 36 Solved: 18
[ Submit][ Status][ Web Board]
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*N matrix, while each element can be either balloons or blank.
Suppose element A and element B are both balloons. They are connected if:
i) They are adjacent;
ii) 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 1represents 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.
Sample Input
5
11001
00100
11111
11010
10010
0
Sample Output
Case 1: 3 2
HINT
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n;
char a[105][105];
int idx[105][105];
int idy[105][105];
void dfsx(int r,int c,int id)
{
if(r<0||c<0||r>=n||c>=n) return;
if(idx[r][c]>0||a[r][c]=='0') return ;
idx[r][c]=id;
for(int dr=-1;dr<=1;dr++)
for(int dy=-1;dy<=1;dy++)
{
if(abs(dr)!=abs(dy))
{
dfsx(r+dr,c+dy,id);
}
}
}
void dfsy(int r,int c,int id)
{
if(r<0||c<0||r>=n||c>=n) return ;
if(idy[r][c]>0||a[r][c]=='0') return ;
idy[r][c]=id;
for(int dr=-1;dr<=1;dr++)
for(int dy=-1;dy<=1;dy++)
{
if(dr!=0||dy!=0)
dfsy(r+dr,c+dy,id);
}
}
int main()
{
int countn=0;
while(cin>>n&&n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
memset(idx,0,sizeof(idx));
memset(idy,0,sizeof(idy));
int cnt1=0,cnt2=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(idx[i][j]==0&&a[i][j]=='1')
{
dfsx(i,j,++cnt1);}
if(idy[i][j]==0&&a[i][j]=='1')
dfsy(i,j,++cnt2);
}
cout<<"Case "<<++countn<<": "<<cnt1<<" "<<cnt2<<endl;
cout<<endl;
}
return 0;
}
1931: Shopping
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 32 Solved: 20
[ Submit][ Status][ Web Board]
Description
Saya and Kudo go shopping together.
You can assume the street as a straight line, while the shops are some points on the line.
They park their car at the leftmost shop, visit all the shops from left to right, and go back to their car.
Your task is to calculate the length of their route.
Input
The input consists of several test cases.
The first line of input in each test case contains one integer N (0<N<100001), represents the number of shops.
The next line contains N integers, describing the situation of the shops. You can assume that the situations of the shops are non-negative integer and smaller than 2^30.
The last case is followed by a line containing one zero.
Output
For each test case, print the length of their shopping route.
Sample Input
4
24 13 89 37
6
7 30 41 14 39 42
0
Sample Output
152
70
HINT
Explanation for the first sample: They park their car at shop 13; go to shop 24, 37 and 89 and finally return to shop 13. The total length is (24-13) + (37-24) + (89-37) + (89-13) = 152
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int a[100005];
int main()
{
int n;
while(cin>>n&&n)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
cout<<(a[n-1]-a[0])*2<<endl;
}
return 0;
}