// File: /workspace/github_cpp_makefile_star2_O2/neubig[P]egret/Egret/src/Binarization.cpp
// Function: binarize(Rule&, InterNodes&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Direct)
int binarize(Rule& rule, InterNodes& interNodes,string prefix, Direct dir)
{
	vector<string> wrds;
	wrds.clear();
	stringstream rss(rule.right.c_str());
	while(!rss.eof())
	{
		string curWrd="";
		rss>>curWrd;
		if(curWrd=="")break;
		wrds.push_back(curWrd);
	}
	vector<vector<string> > strMatrix;
	strMatrix.clear();
	for(int i=0;(size_t)i<wrds.size();i++)
	{
		vector<string> temp;
		strMatrix.push_back(temp);
		for(int j=0;(size_t)j<wrds.size();j++)
		{
			if(i==j)
				strMatrix[i].push_back(wrds[i]);
			else
				strMatrix[i].push_back("");
		}
	}
	for(int i=0;(size_t)i<wrds.size();i++)
	{
		for(int j=i+1;(size_t)j<wrds.size();j++)
		{
			strMatrix[i][j]=strMatrix[i][j-1]+wrds[j];
		}
	}
	jout<<"rightPart length:"<<(unsigned int)wrds.size()<<endl;

	for(int k=(int)wrds.size();k>1;k--)
	{
		for(int i=0;i<=(int)wrds.size()-k;i++)
		{
			if(dir==LEFT&&i!=wrds.size()-k)continue;
			if(dir==RIGHT&&i!=0)break;
			for(int j=i;j<i+k-1;j++)
			{
				if(dir==LEFT&&j!=i)break;
				if(dir==RIGHT&&j!=i+k-2)continue;
				cout<<i<<","<<j<<","<<k<<":["<<i<<","<<j<<"] ["<<j+1<<","<<i+k-1<<"]"<<endl;
				
				string par="";
				if(k==wrds.size())
					par=rule.left;
				else
					par=interNodes.query(strMatrix[i][i+k-1],prefix);
				string leftSon=strMatrix[i][j];
				if(j-i>0)
					leftSon=interNodes.query(leftSon,prefix);
				string rightSon=strMatrix[j+1][i+k-1];
				if(i+k-1-j-1>0)
					rightSon=interNodes.query(rightSon,prefix);
				Rule newRule;
				newRule.left=par;
				newRule.right=leftSon+" "+rightSon;
				if(dir==PERMUTE)
					newRule.prob=double(1)/(double)(k-1);
				else 
					newRule.prob=1;
				if(k==wrds.size())
					newRule.prob*=rule.prob;
				//newRule.print(cout);
				//cout<<endl;
			}
		}
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movl %ecx, 0xc(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, %r14
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
callq 0x515e
movq %r14, 0xa0(%rsp)
movq 0x20(%r14), %rsi
leaq 0xa8(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x9f86
leaq 0x170(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
pushq $0x18
popq %rdx
callq 0x3460
leaq 0xa8(%rsp), %r14
movq %r14, %rdi
callq 0x3578
leaq 0x21ce4(%rip), %r15     # 0x32eb6
leaq 0x70(%rsp), %r12
leaq 0x170(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq 0x170(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x190(%rsp,%rax)
jne 0x11244
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x9f86
movq %r13, %rdi
movq %r14, %rsi
callq 0x3380
movq %r14, %rdi
movq %r15, %rsi
callq 0x57b9
testb %al, %al
jne 0x11237
movq %rbp, %rdi
movq %r14, %rsi
callq 0x129cc
movq %r14, %rdi
callq 0x3578
jmp 0x111e4
leaq 0xa8(%rsp), %rdi
callq 0x3578
xorpd %xmm0, %xmm0
leaq 0x50(%rsp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
callq 0x129f8
xorl %r15d, %r15d
leaq 0x70(%rsp), %rbx
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x5, %rcx
cmpq %r15, %rcx
jbe 0x1132d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
andq $0x0, 0x80(%rsp)
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x12a00
imulq $0x18, %r15, %r12
movq %r15, %rbp
shlq $0x5, %rbp
xorl %r13d, %r13d
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %r13, %rax
jbe 0x11318
movq 0x50(%rsp), %r14
cmpq %r13, %r15
jne 0x112de
addq %r12, %r14
addq %rbp, %rsi
movq %r14, %rdi
callq 0x129cc
jmp 0x11313
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x21bc6(%rip), %rsi     # 0x32eb6
leaq 0xf0(%rsp), %rdx
callq 0x9f86
addq %r12, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6f64
movq %rbx, %rdi
callq 0x3578
incq %r13
jmp 0x112ae
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
callq 0x5166
incq %r15
jmp 0x11263
pushq $0x1
popq %rbx
pushq $0x20
popq %r12
xorl %r13d, %r13d
leaq 0xa8(%rsp), %r14
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x5, %rcx
cmpq %r13, %rcx
jbe 0x113c8
imulq $0x18, %r13, %rbp
incq %r13
movq %r12, 0x40(%rsp)
movq %rbx, %r15
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x5, %rcx
cmpq %r15, %rcx
jbe 0x113b7
movq 0x50(%rsp), %rax
movq (%rax,%rbp), %rax
leaq (%rax,%r12), %rsi
addq $-0x20, %rsi
addq %r12, %rdx
movq %r14, %rdi
callq 0x12a2c
movq 0x50(%rsp), %rax
movq (%rax,%rbp), %rdi
addq %r12, %rdi
movq %r14, %rsi
callq 0x3360
movq %r14, %rdi
callq 0x3578
incq %r15
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
addq $0x20, %r12
jmp 0x1135d
incq %rbx
movq 0x40(%rsp), %r12
addq $0x20, %r12
jmp 0x1133f
leaq 0x3af29(%rip), %rdi     # 0x4c2f8
leaq 0x20dba(%rip), %rsi     # 0x32190
callq 0x12a64
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
shrq $0x5, %rsi
movq %rax, %rdi
callq 0x12a82
leaq 0x3af10(%rip), %rcx     # 0x4c308
cmpl $0x0, (%rcx)
je 0x11405
movq (%rax), %rdi
callq 0x3130
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x5, %rdx
movslq %edx, %r15
cmpq $0x2, %r15
jl 0x1180c
leaq -0x1(%r15), %rdx
cmpl $0x2, 0xc(%rsp)
movq %rdx, 0x98(%rsp)
je 0x11449
movsd 0x20d47(%rip), %xmm0    # 0x32188
movsd %xmm0, 0x38(%rsp)
jmp 0x1145f
cvtsi2sd %edx, %xmm0
movsd 0x20d33(%rip), %xmm1    # 0x32188
divsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rsp)
xorl %r12d, %r12d
movq %rcx, %rdx
subq %rax, %rdx
movq %rdx, %rsi
shrq $0x5, %rsi
movslq %esi, %rsi
subq %r15, %rsi
cmpq %rsi, %r12
jg 0x117ff
cmpl $0x0, 0xc(%rsp)
jne 0x11495
sarq $0x5, %rdx
subq %r15, %rdx
cmpq %r12, %rdx
jne 0x117f7
cmpl $0x1, 0xc(%rsp)
sete %dl
testq %r12, %r12
setne %sil
testb %sil, %dl
jne 0x117ff
leaq (%r12,%r15), %rax
leaq (%r12,%r15), %rbp
decq %rbp
leal (%r12,%r15), %ecx
decl %ecx
movl %ecx, 0x4c(%rsp)
addl $-0x2, %eax
movq %rax, 0x40(%rsp)
movq %r12, %r13
cmpl $0x0, 0xc(%rsp)
setne %al
cmpq %r12, %r13
sete %cl
cmpq %rbp, %r13
jge 0x117ed
orb %cl, %al
je 0x117ed
cmpl $0x1, 0xc(%rsp)
jne 0x114ff
cmpq 0x40(%rsp), %r13
je 0x114ff
incq %r13
jmp 0x114cd
movq 0x3aaca(%rip), %rdi     # 0x4bfd0
movl %r12d, %esi
callq 0x34d0
movq %rax, %rdi
leaq 0x20b33(%rip), %rsi     # 0x3204b
callq 0x32d0
movq %rax, %rdi
movl %r13d, %esi
callq 0x34d0
movq %rax, %rdi
leaq 0x20b19(%rip), %rsi     # 0x3204b
callq 0x32d0
movq %rax, %rdi
movl %r15d, %esi
callq 0x34d0
movq %rax, %rdi
leaq 0x20c56(%rip), %rsi     # 0x321a2
callq 0x32d0
movq %rax, %rdi
movl %r12d, %esi
callq 0x34d0
movq %rax, %rdi
leaq 0x20ae5(%rip), %rsi     # 0x3204b
callq 0x32d0
movq %rax, %rdi
movl %r13d, %esi
callq 0x34d0
movq %rax, %rdi
leaq 0x20c25(%rip), %rsi     # 0x321a5
callq 0x32d0
leaq 0x1(%r13), %r14
movq %rax, %rdi
movl %r14d, %esi
callq 0x34d0
movq %rax, %rdi
leaq 0x20aad(%rip), %rsi     # 0x3204b
callq 0x32d0
movq %rax, %rdi
movl %ebp, %esi
callq 0x34d0
movq %rax, %rdi
leaq 0x2149e(%rip), %rsi     # 0x32a55
callq 0x32d0
movq %rax, %rdi
callq 0x3130
leaq 0x70(%rsp), %rdi
leaq 0x218e6(%rip), %rsi     # 0x32eb6
leaq 0xa8(%rsp), %rdx
callq 0x9f86
movq 0x18(%rsp), %rcx
subq 0x10(%rsp), %rcx
sarq $0x5, %rcx
movq 0xa0(%rsp), %rax
cmpq %r15, %rcx
je 0x1161b
movq 0x50(%rsp), %rax
imulq $0x18, %r12, %rcx
movq %rbp, %rsi
shlq $0x5, %rsi
addq (%rax,%rcx), %rsi
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rdx
callq 0x11932
leaq 0x70(%rsp), %rdi
movq %rax, %rsi
callq 0x3170
movq 0x50(%rsp), %rax
imulq $0x18, %r12, %rcx
movq %r13, %rsi
shlq $0x5, %rsi
addq (%rax,%rcx), %rsi
leaq 0xf0(%rsp), %rdi
callq 0x33c0
cmpq %r12, %r13
jbe 0x11675
movq 0x28(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x11932
leaq 0xf0(%rsp), %rdi
movq %rax, %rsi
callq 0x3170
movq 0x50(%rsp), %rax
imulq $0x18, %r14, %rcx
movq %rbp, %rsi
shlq $0x5, %rsi
addq (%rax,%rcx), %rsi
leaq 0x110(%rsp), %rdi
callq 0x33c0
subl 0x4c(%rsp), %r13d
cmpl $-0x2, %r13d
jg 0x116c8
movq 0x28(%rsp), %rdi
leaq 0x110(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x11932
leaq 0x110(%rsp), %rdi
movq %rax, %rsi
callq 0x3170
leaq 0xb8(%rsp), %rax
movq %rax, 0xa8(%rsp)
andq $0x0, 0xb0(%rsp)
xorl %ecx, %ecx
movb %cl, 0xb8(%rsp)
leaq 0xd8(%rsp), %rax
movq %rax, 0xc8(%rsp)
andq $0x0, 0xd0(%rsp)
movb %cl, 0xd8(%rsp)
leaq 0xa8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x3170
leaq 0x130(%rsp), %r13
movq %r13, %rdi
leaq 0xf0(%rsp), %rsi
leaq 0x2177c(%rip), %rdx     # 0x32eb2
callq 0xbf0b
leaq 0x150(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x110(%rsp), %rdx
callq 0xbeec
leaq 0xc8(%rsp), %rdi
movq %rbx, %rsi
callq 0x3360
movq %rbx, %rdi
callq 0x3578
movq %r13, %rdi
callq 0x3578
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0xe8(%rsp)
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x5, %rax
cmpq %r15, %rax
jne 0x117b4
movq 0xa0(%rsp), %rax
movsd 0x40(%rax), %xmm0
mulsd 0x38(%rsp), %xmm0
movsd %xmm0, 0xe8(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x12aa2
leaq 0x110(%rsp), %rdi
callq 0x3578
leaq 0xf0(%rsp), %rdi
callq 0x3578
leaq 0x70(%rsp), %rdi
callq 0x3578
movq %r14, %r13
jmp 0x114cd
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
incq %r12
jmp 0x11462
movq 0x98(%rsp), %r15
jmp 0x1141c
leaq 0x50(%rsp), %rdi
callq 0x12ab8
leaq 0x170(%rsp), %rdi
callq 0x3260
leaq 0x10(%rsp), %rdi
callq 0x5166
xorl %eax, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x3578
jmp 0x11920
movq %rax, %rbx
jmp 0x11920
jmp 0x11906
movq %rax, %rbx
jmp 0x11913
jmp 0x11896
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x3578
jmp 0x11913
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x3578
jmp 0x11899
jmp 0x11896
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x5166
jmp 0x11909
jmp 0x11906
jmp 0x118f7
jmp 0x118ab
movq %rax, %rbx
jmp 0x118e6
movq %rax, %rbx
jmp 0x118d9
jmp 0x118b7
movq %rax, %rbx
jmp 0x118cc
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x3578
leaq 0xa8(%rsp), %rdi
callq 0x12aa2
leaq 0x110(%rsp), %rdi
callq 0x3578
leaq 0xf0(%rsp), %rdi
callq 0x3578
jmp 0x118fa
jmp 0x11906
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x3578
jmp 0x11909
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x12ab8
leaq 0x170(%rsp), %rdi
callq 0x3260
leaq 0x10(%rsp), %rdi
callq 0x5166
movq %rbx, %rdi
callq 0x34f0