c++ - What's the reason of int* conversion not permitted to int error? -
#include <iostream> #include <algorithm> #include <string> using namespace std; int minimu ( int a, int b, int c ); int main (void) { int t,i,j; cin>>t; while ( t != 0 ) { string a; string b; cin>>a>>b; int k; int len1 = a.length(); int len2 = b.length(); int dp[len1][len2]; dp[0][0] = 0; ( = 0; < len1; i++ ) { dp[i][0] = i; } ( j = 0; j < len2; j++ ) { dp[0][j] = j; } ( = 1; < len1; i++ ) ( j = 1; j < len2; j++ ) { if ( a[i] == b[j] ) k = 2; else k = 0; dp[i][j] = minimu(dp[i-1,j]+1,dp[i,j-1]+1,dp[i-1][j-1]+k); } cout<<dp[len1-1][len2-1]<<"\n"; t--; } homecoming 0; } int minimu ( int a, int b, int c ) { int foo = min(a,min(b,c)); homecoming foo; }
this programme calculating minimum number of operations while converting 1 string string. allowed operations converting are:
1. deleting character 2. inserting character 3. substituting character
now, when compile program, shows error that:
invalid conversion int* int[-fpermissive]
in line update 2-d array dp[i,j]
. why happening?
because of declaration:
int dp[len1][len2];
dp[i-1,j]
array, not element of matrix. (here comma operator
can confusing, @bartoszkp has explained.)
perhaps
minimu(dp[i-1,j]+1,dp[i,j-1]+1,dp[i-1][j-1]+k);
should be
minimu(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+k); // ^ ^
p.s. please note int dp[len1][len2];
non-const sizes not valid according c++ standard, @cheers , hth said.
c++
No comments:
Post a Comment