Codeforces 612A the Text splitting (extended Euclidean)

Source: Internet
Author: User

A. The Text splitting

You are given the string s of length n and the numbers p, Q. Split the string s to pieces of length p and q.

For example, the string "Hello" for p = 2, q = 3 can is split to the strings " hel "and" lo "or to the strings"He "and"Llo ".

Note It is allowed-split the string s to the strings only of length p or to the strings only of length q (see the second sample test).

Input

The first line contains three positive integers n, p, q (1≤ P, Q≤ n ≤100).

The second line contains the string s consists of lowercase and uppercase Latin letters and digits.

Output

If it ' s impossible to split the string s to the strings of length p and q Print the Only Number "-1".

Otherwise in the first line print integer k -the number of strings in partition of s.

Each of the next K lines should contain the strings in partition. Each string should is of the length p or q. The string should is in the order of their appearing in string s -from.

If There is several solutions print any of them.

Sample Test (s) input
5 2 3
Hello
Output
2
He
Llo
Input
10 9 5
Codeforces
Output
2
Codef
Orces
Input
6 4 5
Privet
Output
-1
Input
8 1 1
Abacabac
Output
8
A
B
A
C
A
B
A
C

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 voidEXGCD (intAintBint& D,int& X,int&y)6 {7     if(!b) d=a,x=1, y=0;8     ElseEXGCD (b,a%b,d,y,x), y-=x* (A/b);9 }Ten intMain () One { A     Chars[ the]; -     intn,p,q,d,x,y; -     BOOLOK; thescanf"%d%d%d",&n,&p,&q); -scanf"%s", s); -     intt=0; -     if(n%p==0) +     { -N/=p; +printf"%d\n", n); A          for(intI=0; i<n;i++) at         { -              for(intj=0; j<p;j++) -printf"%c", s[t++]); -Puts""); -         } -     } in     Else if(n%q==0) -     { toN/=Q; +printf"%d\n", n); -          for(intI=0; i<n;i++) the         { *              for(intj=0; j<q;j++) $printf"%c", s[t++]);Panax NotoginsengPuts""); -         } the     } +     Else A     { theok=1; + EXGCD (p,q,d,x,y); -         if(n%d!=0) $         { $Puts"-1"); -             return 0; -         } the         Else -         {Wuyix*=n/d,y*=n/D; the             inta=p/d,b=q/D; -             inttx=x,ty=y; Wu              while(x<0|| y<0) -             { Aboutx+=b; $y-=A; -                 if(tx*x<0&&ty*y<0) -                 { -                      while(x<0|| y<0) A                     { +x-=b; they+=A; -                         if(tx*x<0&&ty*y<0) $                         { theok=0; the                              Break; the                         } the                     } -                      Break; in                 } the             } the         } About         if(OK) the         { theprintf"%d\n", x+y); the              for(intI=0; i<x;i++) +             { -                  for(intj=0; j<p;j++) theprintf"%c", s[t++]);BayiPuts""); the             } the              for(intI=0; i<y;i++) -             { -                  for(intj=0; j<q;j++) theprintf"%c", s[t++]); thePuts""); the             } the         } -         Else thePuts"-1"); the     } the     return 0;94}

Codeforces 612A the Text splitting (extended Euclidean)

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.