CSU1581: Clock Pictures(KMP)

Description

Input

Output

Sample Input

6
1 2 3 4 5 6
7 6 5 4 3 1

Sample Output

impossible

HINT

Source

NCPC 2014

题意:给出时针的角度,看两个时钟是否匹配

思路:对于连续数字的匹配问题,明显KMP搞起,next数组的运用

#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("%s\n",flag?"possible":"impossible");
    }

    return 0;
}
时间: 2024-10-06 04:23:41

CSU1581: Clock Pictures(KMP)的相关文章

【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

KMP Client README

KMP Client README About KMP is a mod for v0.22 of Kerbal Space Program that adds a multiplayer game option. In a KMP game you can freely interact with other players and do all the Kerbally things you'd normally do in KSP, but with friends (or strange

Clock Pictures

Clock Pictures 题目描述 You have two pictures of an unusual kind of clock. The clock has n 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 i

HDU6513/CCPC2017--A Secret(KMP)

A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total Submission(s): 461    Accepted Submission(s): 182 Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,whi

UVA 11475 后缀数组/KMP

题目链接: 题意:给定一个只含字母的字符串,求在字符串末尾添加尽量少的字符使得字符串为回文串. 思路:因为只能从末尾添加字符,所以其实求的是最长的后缀回文串.那么添加的字符为除了这个原串的最长后缀回文串之外的其他字符.于是问题就转变成了求字符串的最长后缀回文串,对于后缀数组求回文串子串的做法,将整个字符串反过来写在原字符串后面,中间用一个特殊的字符隔开.这样就把问题变为了求这个新的字符串的某两个后缀的最长公共前缀.奇数长度和偶数长度的分开做.对于求奇数长度,假设现在枚举的位置为i,那么对应在反过

CodeForcesGym 100502H Clock Pictures

Clock Pictures Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Original ID: 100502H64-bit integer IO format: %I64d      Java class name: (Any) You have two pictures of an unusual kind of clock. The clock has n

POJ 2406 KMP/后缀数组

题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就能求出循环次数. #define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<str

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

hiho 1015 KMP算法 &amp;&amp; CF 625 B. War of the Corporations

#1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一只河蟹,于是河蟹就向小Hi和小Ho提出了那个经典的问题:“小Hi和小Ho,你们能不能够判断一段文字(原串)里面是不是存在那么一些……特殊……的文字(模式串)?” 小Hi和小Ho仔细思考了一下,觉得只能想到很简单的做法,但是又觉得既然河蟹先生这么说了,就