题意:
给出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;
}