反向功能超出了我的 cpp 程序的范围

reverse function is out of scope in my cpp program

本文关键字:cpp 程序 范围 我的 功能      更新时间:2023-10-16
#include <iostream>
#include <bits/stdc++.h>
#include <cstring>
using namespace std;
int LCS(string x, string y, int n, int m)
{
int t[n + 1][m + 1];
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
{
if (i == 0 || j == 0)
t[i][j] = 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
if (x[i - 1] == y[j - 1])
t[i][j] = 1 + t[i - 1][j - 1];
else
t[i][j] = max(t[i - 1][j], t[i][j - 1]);
}
return t[n][m];
}
int main()
{
string x;
string y = reverse(x.begin(), x.end());
cin >> x;
cout >> LCS(x, y, x.length(), y.length());
return 0;
}

op shows that the C:UserssagarVideoscpp_worklongest pallindromic subsequence LPAmain.cpp|29|error: conversion from 'void' to non-scalar type 'std::string' {aka 'std::__cxx11::basic_string<char>'} requested|

你应该在反转之前请求字符串,cin >> x;应该在使用std::reverse之前询问字符串,因为它是空的。

此行

cout>>LCS(x,y,x.length(),y.length());

有错别字,应该是cout << ....

std::reverse不会返回反转的字符串,它会就地反转它,您不能将其分配给另一个std::string。这是您显示的错误的来源。

字符串x将被反转。

您可能需要类似以下内容:

string x;
string y;
cin >> x; //input x
y = x; //make a copy of x
reverse(y.begin(), y.end()); // reverse y
cout << LCS(x, y, x.length(), y.length());

其他注意事项:

C++标准不允许可变长度数组,int t[n + 1][m + 1];C++无效,尽管一些编译器允许使用它。

不建议使用using namespace std;

因为不是#include <bits/stdc++.h>.