http://218.194.91.48/acmhome/problemdetail.do?&method=showdetail&id=1445
枚举0~len-1位置,如果一个方向不可行,则换另一个方向。
中间写搓了好几次 = =。。。
#include<stdio.h>
#include<string.h>
int main()
{
int t,tt,i,j,ans,temp;
char str[1005];
scanf("%d",&t);
for(tt=1;tt<=t;++tt){
scanf("%s",str);
int len=strlen(str);
ans=0;
for(i=0;i<len;++i){
temp=0;
for(j=i;j<len;++j){
if(str[j]=='C') temp++;
else temp--;
if(temp<0) break;
}
if(temp>=0){
for(j=0;j<i;++j){
if(str[j]=='C') temp++;
else temp--;
if(temp<0) break;
}
if(temp>=0){
ans++;
continue; //该位置符合情况,枚举下一个位置
}
}
temp=0; //否则换一个方向枚举
for(j=i-1;j>=0;--j){
if(str[j]=='C') temp++;
else temp--;
if(temp<0) break;
}
if(temp>=0){
for(j=len-1;j>i;--j){
if(str[j]=='C') temp++;
else temp--;
if(temp<0) break;
}
if(temp>=0) ans++;
}
}
printf("Case %d: %d\n",tt,ans);
}
return 0;
}
本文提供了一道ACM编程竞赛题目的解决方案,采用C语言实现。通过枚举字符串中的每个位置,判断从当前位置出发是否能形成有效的匹配串,并考虑了正反两个方向的可能性。
1742

被折叠的 条评论
为什么被折叠?



