1 条题解

  • 0
    @ 2024-12-24 10:06:03

    C++ :

    #include <stdio.h>
    #include <string.h>
    char s[1000];
    int count(int begin,int end)//计算begin到end所需的步数 
    {//向左跳再向右跳只能回到原位,所以直接考虑向右跳即可,向右跳的方法最多一种 
    	int star,i,x;
    	if(begin>=end)
    		return 0;
    	star=begin;
    	while(star<end&&s[star]!='*')//找星号 
    		star++;
    	if(star*2>begin+end)
    		return -9;//中间没有星号,不能到达 
    	for(i=star+1;i<=star*2-begin;i++)
    		if(s[i]=='*')
    			return -9;
    	x=count(i-1,end);
    	if(x>=0)
    		return x+1;
    	else
    		return -9;
    }
    void run()
    {
    	int a=-1,b,n,i;
    	n=strlen(s)-1;
    	for(i=0;i<=n;i++)//跳的路线是可逆的,所以从a跳到b和从b跳到a等效,只考虑左往右即可 
    		if(s[i]=='a'||s[i]=='b')
    		{
    			if(a==-1)
    				a=i;
    			else
    				b=i;
    		}
    	i=count(a,b);
    	if(i<0) 
    		printf("0\n");
    	else
    		printf("%d\n",i); 
    }
    int main()
    {
    	while(scanf("%s",s)!=EOF)
    		run();
    	return 0;
    } 
    
    
    • 1

    信息

    ID
    1837
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者