http://codeforces.com/contest/864/problem/B
题意:
给出一个字符串,要求找到一个集合S,使得从S中选出的所有数,在这些数的位置上的字母全部为小写且是不同的字母,并且任意在S中两个数i,j,对于i < j,从i到j的所有位置上都为小写字母。
思路:
找出所有的大写字母的位置,在任意两个“相邻”(指的是这两个大写字母的中间不存在大写字母)的大写字母中寻找S,这样就可以保证全部为小写字母,用set保存出现过的字母就可以了。
代码:
1 #include <stdio.h> 2 #include <algorithm> 3 #include <set> 4 using namespace std; 5 6 int pos[205]; 7 char a[205]; 8 9 set<int> s; 10 set<char> c; 11 12 int main() 13 { 14 int n; 15 16 scanf("%d",&n); 17 18 scanf("%s",a); 19 20 pos[0] = -1; 21 int cnt = 1; 22 23 for (int i = 0;i < n;i++) 24 { 25 if (a[i] >= 'A' && a[i] <= 'Z') 26 { 27 pos[cnt++] = i; 28 } 29 } 30 31 pos[cnt] = n; 32 33 int ans = 0; 34 35 for (int i = 0;i < cnt;i++) 36 { 37 c.clear();s.clear(); 38 39 int st = pos[i]+1,en = pos[i+1] - 1; 40 41 for (int j = st;j <= en;j++) 42 { 43 if (c.find(a[j]) == c.end()) 44 { 45 c.insert(a[j]); 46 s.insert(j); 47 } 48 } 49 50 ans = max((int)s.size(),ans); 51 } 52 53 printf("%d ",ans); 54 55 return 0; 56 }