Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- $str='(2*3*y)(/(4*6+x)';
- var_dump(is_valid($str));
- function is_valid($str){
- $bal=0;
- for($i=0,$size=strlen($str);$i<$size;++$i){
- if($str[$i]=='(')
- $bal++;
- elseif($str[$i]==')')$bal--;
- if($bal<0)return false;
- }
- return ($bal==0);
- }
- //генератор скобок
- $n=2;//длина
- $m=2;
- $a=array();
- $out=array();
- rec(0,0);
- print_r($out);
- function out(){
- global $a,$n,$out;
- $tmp='';
- for($i=0;$i<2*$n;++$i){
- $tmp.=$a[$i];
- }
- $out[]=$tmp;
- }
- function rec($inx,$bal){
- global $a,$m,$n,$used;
- if($inx==2*$n){
- if($bal==0){
- out();
- }
- return;
- }
- $a[$inx]='(';
- rec($inx+1,$bal+1);
- if($bal==0) return;
- $a[$inx]=')';
- rec($inx+1,$bal-1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement