Zeros and Ones

本文介绍了一个字符串构造问题,任务是判断给定的0和1组成的字符串是否可以通过0、01或11这几个基本字符串拼接而成。文章给出了一个简洁的解决方案,并通过示例展示了程序的运行效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述
Consider strings of characters made up by concatenating any number of the strings 0, 01 or 11.
For example 00011111 is one such string, as is 001011, but 1011 is not.  Your job is simply to determine if a given string can be constructed in this manner.
输入
 There will be multiple problem sets. Input for each problem will be on one line. Each line (except


the last) will be of the form
n s
where n is a non-negative integer no larger than 100 and s is a string of 0’s and 1’s of length n.
A value of n = 0 indicates end of input.
输出
 Each problem should generate one line of code, either,


String m can be generated.
or
String m can not be generated.
accordingly, where m is the number of the problem (stating at 1).
样例输入
8 00011111
6 001011
4 1011
0
样例输出
String 1 can be generated.
String 2 can be generated.
String 3 can not be generated.


题目意思是输入一个由0,1组成的字符串检测其是否能由0,01,11这几个字符串构成。
规律就是只有是奇数个1开头的字符串不符合题意。源代码如下:

#include<iostream>
using namespace std;
int main()
{
    int n,j=1,i;
    char s[102];
    while(cin>>n&&n)
    {
        for(i=0;i<n;i++)
        cin>>s[i];
        for(i=0;s[i]=='1'&&i<n;i++);
        if(i%2)
            cout<<"String "<<j<<" can not be generated."<<endl;
        else cout<<"String "<<j<<" can be generated."<<endl;
            j++;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值