希望的海洋

Ain't about how fast I get there,ain't about what's waiting on the other side

常用链接

统计

最新评论

DS_stack忆往昔——经常做错的一道铁路调度

shit!太弱了!!警醒!这回思路完全正确、结构也很清晰。但是还是会忽略细节。习惯太差!继续打基础!!Go on!Keep up!
stack 模拟,一个数组就可以搞定.
#include<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<cmath>
#include
<algorithm>

int n,arr[100],stack[100],top=0;
char str[100];

void s2i(char* s)
{
    
int i=0;
    
while(*s)
    
{
        arr[i
++]=*s-'0';
        s
++;
    }

}

void judge(int* ai)
{
    
int i,*pi=ai;
    
for(i=1;i<=n;i++)
    
{
        
if(*pi==i)
        
{
            pi
++;
            
continue;
        }

        
else if(*pi>i)//push i
        {
            stack[top
++]=i;
            
continue;
        }

        
else if(*pi<i)//pop i
        {
            
if(stack[--top]==*pi)
            
{
                pi
++;
                i
--;//忘记此处,这时i没有处理,需恢复
            }

            
else 
            
{
                printf(
"no\n");
                
return;
            }

        }

    }

    
while(top>0)
    
{
        
if(*pi!=stack[--top])
        
{
            printf(
"no\n");
            
return;
        }

        
else pi++;
    }

    printf(
"yes\n");
}




int main()
{
    
int t,k;
    scanf(
"%d",&t);
    
while(t--)
    
{
        top
=0;
        memset(str,
0,sizeof(str));//此三处一再忘记复位,WA无数次。。!!!!!
        memset(stack,0,sizeof(stack));
        scanf(
"%d",&n);
        scanf(
"%s",str);
        s2i(str);
        judge(arr);
    }

    
return 0;
}

posted on 2011-08-24 18:14 拥梦的小鱼 阅读(167) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理