【KMP】【最小表示法】NCPC 2014 H clock pictures

题目链接:

  http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794

题目大意:

  两个无刻度的钟面,每个上面有N根针(N<=200000),每个针都是相同的,分别指向Ai,Bi(360°被分成360000小份),问能否将其中一个旋转和另一个重合。

题目思路:

  【KMP】【最小表示法】

  循环同构问题。可以写KMP,我懒得写KMP了就写了循环同构的最小表示法。

  首先将Ai排序,然后求差(记得取模360000,WA了一次),接下来复制一遍开始匹配。

  A从第I位开始匹配,B从第J位开始匹配,匹配了k位发现不相同的时候,如果A[i+k]>B[j+k],将I移至I+K+1,否则J移至J+K+1.直到匹配N位或无解。

 

 1 //
 2 //by coolxxx
 3 //#include<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<stack>
10 #include<queue>
11 #include<set>
12 #include<bitset>
13 #include<memory.h>
14 #include<time.h>
15 #include<stdio.h>
16 #include<stdlib.h>
17 #include<string.h>
18 //#include<stdbool.h>
19 #include<math.h>
20 #define min(a,b) ((a)<(b)?(a):(b))
21 #define max(a,b) ((a)>(b)?(a):(b))
22 #define abs(a) ((a)>0?(a):(-(a)))
23 #define lowbit(a) (a&(-a))
24 #define sqr(a) ((a)*(a))
25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
26 #define mem(a,b) memset(a,b,sizeof(a))
27 #define eps (1e-8)
28 #define J 10
29 #define mod 360000
30 #define MAX 0x7f7f7f7f
31 #define PI 3.14159265358979323
32 #define N 400004
33 using namespace std;
34 typedef long long LL;
35 int cas,cass;
36 int n,m,lll,ans;
37 int a[N],b[N],aa[N],bb[N];
38 bool cmp(int aa,int bb)
39 {
40     return aa<bb;
41 }
42 int main()
43 {
44     #ifndef ONLINE_JUDGE
45 //    freopen("1.txt","r",stdin);
46 //    freopen("2.txt","w",stdout);
47     #endif
48     int i,j,k;
49     
50 //    for(scanf("%d",&cass);cass;cass--)
51 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
52 //    while(~scanf("%s",s+1))
53     while(~scanf("%d",&n))
54     {
55         for(i=1;i<=n;i++)
56             scanf("%d",&aa[i]);
57         for(i=1;i<=n;i++)
58             scanf("%d",&bb[i]);
59         sort(aa+1,aa+1+n,cmp);
60         sort(bb+1,bb+1+n,cmp);
61         aa[n+1]=aa[1],bb[n+1]=bb[1];
62         for(i=1;i<=n;i++)
63             a[i+n]=a[i]=(aa[i+1]-aa[i]+mod)%mod,b[i+n]=b[i]=(bb[i+1]-bb[i]+mod)%mod;
64         for(i=1,j=1;i<=n && j<=n;)
65         {
66             for(k=0;k<n;k++)
67             {
68                 if(a[i+k]!=b[j+k])
69                 {
70                     if(a[i+k]>b[i+k])
71                     {
72                         i=i+k+1;
73                         break;
74                     }
75                     else
76                     {
77                         j=j+k+1;
78                         break;
79                     }
80                 }
81             }
82             if(k==n)break;
83         }
84         if(k==n)puts("possible");
85         else puts("impossible");
86     }
87     return 0;
88 }
89 /*
90 //
91 
92 //
93 */
View Code

 

posted @ 2016-08-31 20:06  Cool639zhu  阅读(461)  评论(0编辑  收藏  举报