Codeforces Round #302 (Div. 2) A. Set of Strings 水题

A. Set of Strings

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/544/problem/A

Description

You are given a string q. A sequence of k strings s1, s2, ..., sk is called beautiful, if the concatenation of these strings is string q (formally, s1 + s2 + ... + sk = q) and the first characters of these strings are distinct.

Find any beautiful sequence of strings or determine that the beautiful sequence doesn't exist.


Input

The first line contains a positive integer k (1 ≤ k ≤ 26) — the number of strings that should be in a beautiful sequence.

The second line contains string q, consisting of lowercase Latin letters. The length of the string is within range from 1 to 100, inclusive.

Output

If such sequence doesn't exist, then print in a single line "NO" (without the quotes). Otherwise, print in the first line "YES" (without the quotes) and in the next k lines print the beautiful sequence of strings s1, s2, ..., sk.

If there are multiple possible answers, print any of them.

Sample Input

1
abca

Sample Output

YES
abca

HINT

 In the second sample there are two possible answers: {"aaaca", "s"} and {"aaa", "cas"}.

题意

 把一个字符串能不能拆成N份,要求每一份的开头字母都不相同

题解:

统计一下有多少个不同的字母,如果小于n,那就直接输出no

否则就输出这些分开的字符串就好了~

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1000001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

string s;
map<char,int> H;
int flag[maxn];
int main()
{
    int n=read();
    int ans=0;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        if(H[s[i]])
            continue;
        H[s[i]]=1;
        flag[ans]=i;
        ans++;
    }
    if(ans<n)
    {
        puts("NO");
        return 0;
    }
    puts("YES");
    for(int j=0;j<n-1;j++)
    {
        for(int i=flag[j];i<flag[j+1];i++)
            cout<<s[i];
        cout<<endl;
    }
    for(int i=flag[n-1];i<s.size();i++)
        cout<<s[i];
    cout<<endl;
}
原文地址:https://www.cnblogs.com/qscqesze/p/4487314.html