您的位置:首页 > 其它

CF 2014 Nordic Collegiate Programming Contest H.Clock Pictures

2014-10-28 17:52 337 查看
http://codeforces.com/gym/100502

H.Clock Pictures

You have two pictures of an unusual kind of clock. The clock hasn
hands, each having the

same length and no kind of marking whatsoever. Also, the numbers on the clock are so faded

that you can’t even tell anymore what direction is up in the picture. So the only thing that you

see on the pictures, aren
shades of then
hands, and nothing else.

You’d like to know if both images might have been taken at exactly the same time of the day,

possibly with the camera rotated at different angles.

Task

Given the description of the two images, determine whether it is possible that these two pictures

could be showing the same clock displaying the same time.

Input

The first line contains a single integern
(2≤
n≤200000),
the number of hands on the clock.

Each of the next two lines containsn
integersa
i(0≤
ai<360000),
representing the

angles of the hands of the clock on one of the images, in thousandths of a degree. The first line

represents the position of the hands on the first image, whereas the second line corresponds

to the second image. The numbera
idenotes the angle
between the recorded position of some

hand and the upward direction in the image, measured clockwise. Angles of the same clock are

distinct and are not given in any specific order.

Output

Output one line containing one word:possible

if the clocks could be showing the same time,

impossibleotherwise.

Sample Input 1

6

1 2 3 4 5 6

7 6 5 4 3 1

Sample Output 1

impossible

Sample Input 2

2

0 270000

180000 270000

Sample Output 2

possible

Sample Input 3

7

140 130 110 120 125 100 105

235 205 215 220 225 200 240

Sample Output 3

impossible

解析

注意时钟角度输入是无序的,所以读入后排个序。然后求DertaDegree[i]。表示这个指针(顺时针方向)到下一个最近指针转过的角度。然后两个时钟的DertaDegree[i]进行一次字符串匹配即可。

第一次写KMP,所以注释比较多。

介绍两篇KMP写得不错的blog

Matrix67 http://www.matrix67.com/blog/archives/115

v_JULY_v
/article/1362918.html

#include<cstdio>
#include<algorithm>

using namespace std;

int N,DDeg1[200100],DDeg2[400100],Deg1[200100],Deg2[200100],Pre[200100];

void readdata()
{
for(int i=1;i<=N;i++)scanf("%d",&Deg1[i]);
sort(Deg1+1,Deg1+1+N);
for(int i=1;i<N;i++) DDeg1[i]=Deg1[i+1]-Deg1[i]; DDeg1
=360000-Deg1
+Deg1[1];

for(int i=1;i<=N;i++)scanf("%d",&Deg2[i]);
sort(Deg2+1,Deg2+1+N);
for(int i=1;i<N;i++) DDeg2[i]=Deg2[i+1]-Deg2[i]; DDeg2
=360000-Deg2
+Deg2[1];
//在DDeg2中寻找子串DDeg1
for(int i=N+1;i<=2*N;i++) DDeg2[i]=DDeg2[i-N];
}

void kmp()
{
//DDeg1为模板串 DDeg2为文本串
//对于Pre[i]我们必须保证满足这样的性质:字符串[1,Pre[i]]是字符串[1,i]的后缀子串

//kmp prework
int j=0; Pre[1]=0;
for(int i=2;i<=N;i++)
{
//j里面保存的是上一格失配后应该跳回的地方
while(j>0 && DDeg1[j+1]!=DDeg1[i]) j=Pre[j];
//这里有两种情况跳出了循环
//1.j==0 那么Pre[i]=0;
//2.DDeg1[j+1]==DDeg1[i] 那么Pre[i]=j+1;
if(DDeg1[j+1]==DDeg1[i]) Pre[i]=++j;
else Pre[i]=0;
}

j=0;
bool flag=0;
for(int i=1;i<=2*N;i++)
{
while(j>0 && DDeg1[j+1]!=DDeg2[i]) j=Pre[j];
if(DDeg1[j+1]==DDeg2[i]) j++;
if(j==N)
{
flag=1;
break; //如果要多次找子串还可以去掉break;
j=Pre[j]; //多次找子串时,使算法进行下去
}
}

if(flag) printf("possible");
else printf("impossible");
}

int main()
{
while(scanf("%d",&N)==1)
{
readdata();
kmp();
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐