The Text Splitting
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
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 be split to the two strings “Hel” and “lo” or to the two strings “He” and “llo”.
Note it is allowed to 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 be of the length p or q. The string should be in order of their appearing in string s — from left to right.
If there are several solutions print any of them.
Sample Input
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
——————————————————————————————————————
因为一开始想错了,所以印象特别深的一题,后来还看了别人的代码⊙﹏⊙。一开始以为是有三种情况 p 的倍数,q的倍数,p+q的倍数(如14 =(3+4)*2),后来发现WA 了,提示Wrong answer on test 9。
后来发现自己错的地方不是细节,而是思路,因为还有 n倍的(p的倍数+q的倍数)这种情况没有考虑(如 22 =(5+3+3)*2)。思路错了,如果一开始想到是这样的话就会直接用两个for,结果费了好大力气。
下面贴上错误和正确的两个代码:
——————————————————————————————————————
错误代码:
//三个搜索……
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
int n,p,q,i,j,a,b,c,t,tt,ttt,k;
char ch[105];
while(~scanf("%d%d%d",&n,&p,&q))
{
k=t=tt=ttt=0;
getchar();
for( i=0; i<n; i++ )
{
ch[i]=0;
}
for( i=0; i<n; i++ )
{
scanf("%c",&ch[i]);
}
a=n%p;
b=n%q;
c=n%(p+q);
if(!a)
{
t=n/p;
printf("%d\n",t);
k=0;
for( j=0; j<t; j++ )
{
for( i=0; i<p; i++ )
{
printf("%c",ch[i+p*k]);
}
printf("\n");
k++;
}
continue;
}
if(!b)
{
tt=n/q;
printf("%d\n",tt);
k=0;
for( j=0; j<tt; j++ )
{
for( i=0; i<q; i++ )
{
printf("%c",ch[i+q*k]);
}
printf("\n");
k++;
}
continue;
}
if(!c)
{
ttt=(n/(p+q))*2;
printf("%d\n",ttt);
k=0;
for( j=0; j<n/(p+q); j++ )
{
for( i=0; i<p; i++ )
{
printf("%c",ch[i+(p+q)*k]);
}
printf("\n");
for( i=p; i<q+p; i++)
{
printf("%c",ch[i+(p+q)*k]);
}
printf("\n");
k++;
}
continue;
}
if( a != 0 && b != 0 && c != 0 )
{
printf("-1\n");
continue;
}
}
return 0;
}
————————————————————————————————————————
正确代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char s[110];
int main()
{
int n,p,q,count,i,j,k;
while( ~scanf("%d%d%d%s",&n,&p,&q,s) )
{
count=-1;
for( i=0; i<=n/p; i++ )
{
for( j=0; j<=n/q; j++ )
if( i*p + j*q == n ) //把每一种可能都试一遍
{
count=i+j; // 前面错误的原因是因为没有考虑多个q 和单个 p,或者单个 p 和多个q
// 如果题意没有理解错,就会想到用两个循环,而不是分开来写
printf("%d\n",count);
for( k=0; k<n; k++ )
{
printf("%c",s[k]);
if( i && k<i*p && !((k+1)%p) || j && k >= i*p &&!( (k-i*p+1)%q) )
printf("\n"); //确定换行的时间
}
break;
}
if( count != -1 )break;
}
if( count == -1 )printf("-1\n");
}
return 0;
}