Clock Pictures(kmp + Contest2075 - 湖南多校对抗赛(2015.04.26))
Problem H: Clock Pictures
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 73 Solved: 18
[Submit][Status][Web Board]
Description
Input
Output
Sample Input
6
1 2 3 4 5 6
7 6 5 4 3 1
Sample Output
impossible
HINT
题意:给你角度,问你通过旋转a图和b图能否重合;
思路:KMP;(比赛的时候,忘记是环了。。。还好队友给力!)
我的代码:
#include<stdio.h> #include<algorithm> #include<string.h> #define mmax 200005 #define mod 360000 #define mem(a) memset(a,0,sizeof(a)); using namespace std; int a[mmax],b[mmax],nnext[mmax]; int c[mmax*2]; int t,n; void getnext() { int i=0,k=-1; nnext[0]=-1; int len=n-1; while(i<len) { if(a[i]==a[k]||k==-1) { i++; k++; nnext[i]=k; } else k=nnext[k]; } } void kmp() { int i=0,j=0; int lena=n-1; int lenc=2*n-1; // int cnt=0; while(i<lenc&&j<lena) { if(a[j]==c[i]||j==-1) { i++; j++; } else j=nnext[j]; } if(j!=lena) t=1; //else // printf("i=%d\tj=%d\n",i,j); } int main() { int i; while(scanf("%d",&n)!=EOF) { mem(a); mem(b); mem(c); // mem(c); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) scanf("%d",&b[i]); sort(a,a+n); sort(b,b+n);//printf("%d\t%d\n",a[i],b[i]); c[n-1]=(b[0]-b[n-1]+mod)%mod; for(i=1;i<n;i++) { a[i-1]=(a[i]-a[i-1]+mod)%mod; b[i-1]=(b[i]-b[i-1]+mod)%mod; c[i-1]=c[i-1+n]=b[i-1]; } getnext(); t=0; kmp(); if(t)printf("impossible\n"); else printf("possible\n"); } return 0; }
队友代码:
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm> using namespace std; #define ls 2*i #define rs 2*i+1 #define up(i,x,y) for(i=x;i<=y;i++) #define down(i,x,y) for(i=x;i>=y;i--) #define mem(a,x) memset(a,x,sizeof(a)) #define w(a) while(a) #define LL long long const double pi = acos(-1.0); #define Len 200005 #define mod 360000 const int INF = 0x3f3f3f3f; #define exp 1e-6 int n,a[Len],b[Len],s[Len*2],next1[Len]; void get_next() { next1[0] = 0; int i; up(i,2,n-1) { int t = i-1; t = next1[t]; w(t!=0 && a[t+1]!=a[i]) t = next1[t]; t++; if(a[t]==a[i]) next1[i] = t; else next1[i] = 0; } } int main() { int i,j,k; w(~scanf("%d",&n)) { up(i,0,n-1) scanf("%d",&a[i]); up(i,0,n-1) scanf("%d",&b[i]); sort(a,a+n); sort(b,b+n); down(i,n-1,1) a[i]=(a[i]-a[i-1]+mod)%mod; up(i,0,n-1) s[i]=s[i+n]=b[i]; down(i,2*n-1,1) s[i]=(s[i]-s[i-1]+mod)%mod; int flag = 0; int pos = 1; get_next(); up(i,1,2*n-1) { if(s[i]!=a[pos]) { int tem = next1[pos-1]; w(tem && s[i]!=a[tem+1]) tem = next1[tem]; tem++; if(a[tem]==s[i]) pos = tem+1; else pos = 1; } else pos++; if(pos == n) { flag = 1; break; } } printf("%d\t%d\n",i,n); printf("%s\n",flag?"possible":"impossible"); } return 0; } /************************************************************** Problem: 1581 User: aking2015 Language: C++ Result: Accepted Time:276 ms Memory:5396 kb ****************************************************************/
转载请注明出处:http://www.cnblogs.com/yuyixingkong/
自己命运的掌控着!