#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|
You should ask for string before you reverse it, cin >> x;
should be before you use std::reverse
, because it's empty.
This line
cout>>LCS(x,y,x.length(),y.length());
has a typo, it should be cout << ...
.
std::reverse
does not return the reversed string, it reverses it in place, you can't assign it to another std::string
. This is the source of the error you show.
The string x
will be reversed.
You may want something like:
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());
Other notes:
The C++ standard does not allow for variable length arrays, int t[n + 1][m + 1];
is not valid C++, though some compilers allow its use.