Codeforces#302-A. Set of Strings-水题

题意:

给出n,给出一个小写字母字符串

要求把字符串分成n个子串,并且每个子串开头第一个字母不相同


思路:

只需要统计一下字符串中种类数,如果小于n,不合法

如果大于等于n,把每个不同的字符作为开头输出一个串就好了。。







#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define INF 9223372036854775807
const __int64 maxn = 300005;
char tm[105];
int vis[27];
int main()
{
    
    int n;
    scanf("%d",&n);
    scanf("%s",tm+1);
    int len =strlen(tm+1);
    
    int i,k;
    for (i=1;i<=len;i++)
    {
        vis[tm[i]-'a']=1;
    }
    int cun=0;
    for (i=0;i<26;i++)
    {
        if (vis[i]) cun++;      //计算种类
    }
    if (n>cun)
    { printf("NO\n");return 0;}
    
    printf("YES\n");
    int need=n;
    for (i=1;i<=len;)
    { 

        if (vis[tm[i]-'a'])     //输出一个没出现过的字符
        {
            printf("%c",tm[i]);
             
            vis[tm[i]-'a']=0;   //标记为出现过
            i++; 
            if (i>len) break;   //注意不要越界
            while (vis[tm[i]-'a']==0)       //只要出现过了就输出/不影响结果
            {
                printf("%c",tm[i]); 
                i++; 
                if (i>len) break;       //注意不要越界
            }
            need--;
            if (!need) break;       //如果已经输出了need串,并且还有剩下的字符串,直接全部输出
            
            printf("\n");
        }
    }   
    for (;i<=len;i++)       //直接输出剩下的全部
    {
        printf("%c",tm[i]);
    }
    printf("\n");
    
    
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值