[BalticOI2014] Friends

Source: Internet
Author: User

[BalticOI2014] Friends topic:

A string \ (a\)that copies \ (a\) over and inserts a new character at any location to get \ (b\). Give the \ (B (| B|\LE2\TIMES10^6,|\SIGMA|\LE26) \ (a\) , whether it exists, is unique. If unique, find \ (a\).


A string hash.

#include <cstdio> #include <cctype>inline int getint () {Register char ch;    while (!isdigit (Ch=getchar ()));    Register int x=ch^ ' 0 ';    while (IsDigit (Ch=getchar ())) x= (((x<<2) +x) <<1) + (ch^ ' 0 '); return x;}    inline char Getalpha () {Register char ch;    while (!isalpha (Ch=getchar ())); return ch;} typedef unsigned long long uint64;const int n=2e6+1;const uint64 base=31;int s[n];uint64 pwr[n],hash[n];inline UInt64 Calc (const int &i,const int &j) {return hash[j]-hash[i-1]*pwr[j-i+1];}    int main () {const int n=getint ();        if (n%2==0) {puts ("not POSSIBLE");    return 0;        } for (register int i=pwr[0]=1;i<=n;i++) {s[i]=getalpha ();        Pwr[i]=pwr[i-1]*base;    hash[i]=hash[i-1]*base+s[i]-' A ' +1;    } UInt64 last=0;    int POS; for (register int i=1;i<=n/2;i++) {if (Calc (1,i-1) *pwr[n/2-i+1]+calc (i+1,n/2+1) ==calc (n/2+2,n)) {if (l Ast!=0&&calc (n/2+2,n)!=last) {puts ("not UNIQUE ");            return 0;            } last=calc (N/2+2,n);        Pos=i;            }} if (Calc (1,N/2) ==calc (n/2+2,n)) {if (Last!=0&&calc (1,N/2)!=last) {puts ("not UNIQUE");        return 0;        } last=calc (1,N/2);    pos=n/2+1; } for (register int i=n/2+2;i<=n;i++) {if (Calc (1,N/2) ==calc (n/2+1,i-1) *pwr[n-i]+calc (i+1,n)) {if (                Last!=0&&calc (1,N/2)!=last) {puts ("not UNIQUE");            return 0;            } last=calc (1,N/2);        Pos=i;        }} if (!last) {puts ("not POSSIBLE");    return 0;    } if (POS&LT;=N/2) {for (register int i=n/2+2;i<=n;i++) Putchar (S[i]);    } else {for (register int i=1;i<=n/2;i++) Putchar (S[i]);    } putchar (' \ n '); return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.