反向函数超出了我的 cpp 程序的范围

reverse function is out of scope in my cpp program

#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:\Users\sagar\Videos\cpp_work\longest pallindromic subsequence LPA\main.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 不是 return 反转的字符串,它是原地反转,你不能将它分配给另一个 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]; is not valid C++,尽管一些编译器允许使用它。

using namespace std; is not recommended.

.