【 CodeForces 612A 】 The Text Splitting

本文介绍了一种字符串拆分算法的问题及解决方法。该算法需要将一个字符串按照指定长度p和q进行拆分,并探讨了不同情况下拆分的可能性及实现方式。

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;
}
### 关于 Codeforces Problem 1804A 的解决方案 Codeforces 是一个广受欢迎的在线编程竞赛平台,其中问题 1804A 可能涉及特定算法或数据结构的应用。尽管未提供具体题目描述,但通常可以通过分析输入输出样例以及常见解法来推导其核心逻辑。 #### 题目概述 假设该问题是关于字符串处理、数组操作或其他基础算法领域的内容,则可以采用以下方法解决[^2]: 对于某些初学者来说,遇到不熟悉的语言(如 Fortran),可能会感到困惑。然而,在现代竞赛环境中,大多数情况下会使用更常见的语言(C++、Python 或 Java)。因此,如果题目提及某种神秘的语言,可能只是为了增加趣味性而非实际需求。 #### 解决方案思路 以下是基于一般情况下的潜在解答方式之一: ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int t; cin >> t; // 输入测试用例数量 while(t--){ string s; cin >> s; // 获取每组测试数据 // 假设这里需要执行一些简单的变换或者判断条件... bool flag = true; // 初始化标志位为真 for(char c : s){ if(c != 'a' && c != 'b'){ flag = false; break; } } cout << (flag ? "YES" : "NO") << "\n"; // 输出结果 } return 0; } ``` 上述代码片段展示了一个基本框架,适用于许多入门级字符串验证类问题。当然,这仅作为示范用途;真实场景下需依据具体要求调整实现细节。 #### 进一步探讨方向 除了官方题解外,社区论坛也是获取灵感的好地方。通过阅读他人分享的经验教训,能够加深对该类型习题的理解程度。同时注意积累常用技巧并灵活运用到不同场合之中[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值