hihocoder 1039 : 字符消除
#include <stdio.h> #include <string.h> const int N = 200; int fun(char *s) { int sum = 0; bool flag = true; while(flag) { flag = false; int len = strlen(s); int map[N]={0}; for(int i = 0; i < len; i++) { if((i+1<len && s[i]==s[i+1])||(i-1>=0 &&s[i]==s[i-1])) { map[i] = 1; } } int tlen = 0; for(int i = 0; i < len; i++) { if(!map[i]) { s[tlen++]=s[i]; } else { sum++; flag = true; } } s[tlen] = 0; } return sum; } int main() { int T = 0; scanf("%d",&T); char line[N]={0}; for(int i = 0; i < T; i++) { scanf("%s",line); int len = strlen(line); int maxn = 0; for(int j = 0; j <= len; j++) { for(char ch = 'A'; ch <= 'C';ch++) { char tmp[N]={0}; int t = 0; for(int k = 0; k < j; k++) tmp[t++] = line[k]; tmp[t++] = ch; for(int k = j; k < len; k++) tmp[t++] = line[k]; tmp[t]=0; int v = fun(tmp); if(maxn < v) maxn = v; } } printf("%d\n",maxn); } return 0; }
posted on 2015-11-20 15:53 zyz913614263 阅读(172) 评论(0) 编辑 收藏 举报