A. Set of Strings
题意:能否把一个字符串划分为n段,且每段第一个字母都不相同?
思路:判断字符串中出现的字符种数,然后划分即可。
1 #include<iostream> 2 #include<set> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 char s[110]; 7 set<char>st; 8 int main() 9 { 10 int n; 11 scanf("%d%s", &n,s); 12 int len = strlen(s); 13 for (int i = 0; i < len; i++) 14 { 15 if (!st.count(s[i]))st.insert(s[i]); 16 } 17 if (st.size() < n) printf("NO\n"); 18 else 19 { 20 st.clear(); 21 printf("YES\n"); 22 int cur = 0; 23 while (n--) 24 { 25 st.insert(s[cur]); 26 while (cur<len&&st.count(s[cur])) 27 { 28 printf("%c", s[cur++]); 29 } 30 if (n == 0) 31 { 32 while(cur<len) printf("%c", s[cur++]); 33 } 34 printf("\n"); 35 } 36 } 37 return 0; 38 }