C++ CYK算法实现C++;
我一直在尝试基于维基百科提供的伪代码实现CYK算法,但是我似乎无法正确实现,我不知道代码出了什么问题。我最好的猜测是我的索引是错误的,因为伪代码没有使用数组索引 这是我正在测试的语法:C++ CYK算法实现C++;,c++,context-free-grammar,chomsky-normal-form,cyk,C++,Context Free Grammar,Chomsky Normal Form,Cyk,我一直在尝试基于维基百科提供的伪代码实现CYK算法,但是我似乎无法正确实现,我不知道代码出了什么问题。我最好的猜测是我的索引是错误的,因为伪代码没有使用数组索引 这是我正在测试的语法: /-->U1V1 | U2V2 | V1V1 | V2V2 | a | b S-->U1V1 | U2V2 | V1V1 | V2V2 | a | b V1-->a V2-->b U1-->V1S U2-->V2S 此语法接受回文,因此“baab”应该被接受,但它不被接受为语法的一部分 代码如下: int n =
/-->U1V1 | U2V2 | V1V1 | V2V2 | a | b
S-->U1V1 | U2V2 | V1V1 | V2V2 | a | b
V1-->a
V2-->b
U1-->V1S
U2-->V2S
此语法接受回文,因此“baab”应该被接受,但它不被接受为语法的一部分
代码如下:
int n = (int)S.size();
std::vector<std::vector<std::vector<bool>>> P(n, std::vector<std::vector<bool>>(n, std::vector<bool>(startVariables.size(), false)));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < productions.size(); j++)
{
std::vector<std::string> prods = getProductionsFromVariable(productions[j].variable);
for(int k = 0; k < prods.size(); k++)
{
if(prods[k][0] == S[i] && !isMayus(prods[k][0]) && !isnumber(prods[k][0]))
{
P[0][i][j] = true;
}
}
}
}
for(int i = 1; i < n; i++)
{
for(int j = 0; j < n-i; j++)
{
for(int k = 0; k < i; k++)
{
for(int l = 0; l < startVariables.size(); l++)
{
std::vector<std::string> prods = getProductionsFromVariable(startVariables[l]);
for(int m = 0; m < prods.size(); m++)
{
if(getNumberOfVars(prods[m]) >= 2)
{
std::vector<std::string> subProds = getNumberedVariables(prods[m]);
int A = getStartVariablePos(startVariables[l]);
int B = getStartVariablePos(subProds[0]);
int C = getStartVariablePos(subProds[1]);
if(P[k][j][B] && P[i-k][j+k][C])
P[i][j][A] = true;
}
}
}
}
}
}
bool cyk = false;
for(int i = 0; i < startVariables.size(); i++)
{
if(P[n-1][0][i])
cyk = true;
}
return cyk;
intn=(int)S.size();
std::vector P(n,std::vector(n,std::vector(startVariables.size(),false));
对于(int i=0;i=2)
{
std::vector subds=getNumberedVariables(prods[m]);
INTA=getStartVariablePos(startVariables[l]);
int B=getStartVariablePos(子程序[0]);
int C=getStartVariablePos(子命令[1]);
if(P[k][j][B]&P[i-k][j+k][C])
P[i][j][A]=真;
}
}
}
}
}
}
bool-cyk=false;
对于(int i=0;i
辅助函数只返回变量的所有给定乘积及其在变量{/,S,V1,V2,U1,U2}向量中的索引该语法(在CNF中)来自转换后的语法S>aSa | bSb | aa | bb | a | b(不在CNF中) 如果您能提供帮助,我们将不胜感激您能提供吗?