foj 1411


一開始想到退棧,沒想到是有點糾結,後來一想應該可以標記所有被匹配的括號,然後查找最大被標記的連續區域。

empty()空爲真 。。。。。

#include <iostream>
#include <stack>
#include <memory.h>
#include <string.h>
 
using namespace std;


struct st{
   int id;
   char c;
}st;

stack <struct st> s;
char str[10010];
int num[10010];

int main()
{

   while(~scanf("%s",str))
   {
    int t=0;
	int n=strlen(str);
    int be,sum=0;
	while(s.empty()==0)
	{
	 s.pop();
	}
	st.c=str[0];
	st.id=0;
	s.push(st);
      int c=0;
	//  memset(num,-1,sizeof(num));
	while(s.empty()==0||t<n)
	{   t++;
	   if(t==n)break;
	   if(s.empty())
	   {
	         num[t]=-1;
		     st.c=str[t];
	        	st.id=t;
		       s.push(st);
	   }
	   else
	   {
		if(s.top().c=='('&&str[t]==')'||s.top().c=='['&&str[t]==']'||s.top().c=='{'&&str[t]=='}'||s.top().c=='<'&&str[t]=='>')
		{
	
          num[s.top().id]=0;
	
		  num[t]=0;
		  s.pop();
		}
		else
		{     num[t]=-1;
		     st.c=str[t];
	        	st.id=t;
		       s.push(st);
		
	   }
	   
	   }
	
	}
	for(int i=1;i<n;i++)
		if(num[i]==0&&num[i-1]==0)
		{  c++;
	    	if(c>sum){be=i;sum=c;}
		
		}
		else
			c=0;
		 if(sum==0)printf("No Solution\n");
		  else
		  {
			  for(int i=0;i<sum+1;i++)
			  {
			    printf("%c",str[be-sum+i]);
			  }
			  printf("\n");
		  }

   }
   return 0;
}


發佈了41 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章