http://codeforces.com/problemset/problem/544/A
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.
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.
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.
1 abca
YES abca
2 aaacas
YES aaa cas
4 abc
NO
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <cstdlib> #include <limits> #include <queue> #include <stack> #include <vector> #include <map> using namespace std; typedef long long LL; #define N 110 #define INF 0x3f3f3f3f #define PI acos (-1.0) #define EPS 1e-8 #define met(a, b) memset (a, b, sizeof (a)) int main () { int n, a[N]; char str[N]; while (scanf("%d %s", &n, str) != EOF) { met (a, 0); for (int i=0; str[i]; i++) a[str[i]-'a']++; int cnt = 0, vis[N]; for (int i=0; i<26; i++) if (a[i]) cnt++; met (vis, 0);//标记一个字符是否在前边的字符串中的第一个字符中出现过 if (cnt >= n) { puts ("YES"); int k = 0, flag = 1; printf ("%c", str[0]); vis[str[k]-'a'] = 1; for (int i=1; str[i]; i++) { if (flag == n)//当是最后一个字符的时候全部输出 { for (int j=i; str[j]; j++) printf ("%c", str[j]); break; } if ((a[str[i]-'a'] != a[str[k]-'a'] || str[i] != str[k]) && !vis[str[i]-'a']) { k = i;//记录该字符串的第一个字符 vis[str[i]-'a'] = 1; flag++; puts (""); } printf ("%c", str[i]); } puts (""); } else puts ("NO"); } return 0; }