[003]Hidden CUGB

本文介绍了一个字符串匹配的问题,即判断一个给定的字符串是否可以通过删除某些字符变成特定目标字符串'CUGB'。文章提供了完整的实现代码,并对特殊情况进行了说明。

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

[003]Hidden CUGB

Time Limit: 1000 ms     Memory Limit: 65536 KB
Description
Given a string of uppercase letters, is it possible to erase ONE OR MORE characters to get the string 'CUGB'?


Input
The first line contains a single integer T (1 ≤ T ≤ 15), the number of test cases. Each test case is a single line containing at least 1 and at most 50 uppercase letters. There are no spaces, TABs, lowercase letters or other characters before or after the string.


Output
For each test case, print the case number and 'Yes' if it is possible to get 'CUGB', or 'No' otherwise. The output is case-sensitive, so don't output 'YES' or 'yes' when 'Yes' should be output.


Sample Input
4
ACMICPCCUGB
ACMICCP
HELLOWORLD
CXXUXXGXXBXX


Sample Output
Case 1: Yes
Case 2: No
Case 3: No
Case 4: Yes

题目大意:

给出一个字符串,判断这个字符串能否通过删除某些字符使得剩余的字符串为CUGB,如果可以,输出Yes并标明是第几组,如果不可以,输出No并标明是第几组。

注意:特殊情况,如果字符串就是CUGB则不用删除字符,此时应该输出No。


代码如下:

#include<stdio.h>
#include<string.h>
int main()
{
	int i,j,k,n,T;
	char str[100],str1[4]={'C','U','G','B'};
	scanf("%d",&T);
	for(i=1;i<=T;i++)
    {
        scanf("%s",str);
        n=strlen(str);
        for(k=0,j=0;j<n;j++)
        {
            if(str[j]==str1[k])
                k++;
        }
        if(k==4&&n>4)
            printf("Case %d: Yes\n",i);
        else
            printf("Case %d: No\n",i);
    }
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值