In the problem WORDHOP :- CodeChef: Practical coding for everyone
I am getting WA in 2 testcases but can’t figure out why, submission :- CodeChef: Practical coding for everyone
#include <bits/stdc++.h>
using namespace std;
vector<string> words;
int n, memo[105];
int solve(int pos)
{
if (memo[pos] != -1)
{
return memo[pos];
}
int tempans = 1;
vector<int> suffix;
suffix.push_back(words[pos][words[pos].length()-1]);
for (int i = words[pos].length()-2; i >= 0; i--)
{
bool done = false;
for (int j = words[pos][i] + 1; j <= 122; j++)
{
if (find(suffix.begin(), suffix.end(), j) != suffix.end())
{
string tempstr = words[pos];
tempstr.erase(tempstr.begin() + i);
tempstr.insert(i, 1, char(j));
if (find(words.begin(), words.end(), tempstr) != words.end())
{
done = true;
tempans = max (tempans, solve(find(words.begin(), words.end(), tempstr) - words.begin()) + 1);
}
}
}
if (!done)
{
string tempstr = words[pos];
tempstr.erase(tempstr.begin() + i);
if (find(words.begin(), words.end(), tempstr) != words.end())
{
tempans = max (tempans, solve(find(words.begin(), words.end(), tempstr) - words.begin()) + 1);
}
}
suffix.push_back(words[pos][i]);
}
return memo[pos] = tempans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string temp;
memset (memo, -1, sizeof memo);
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> temp;
words.push_back(temp);
}
int ans = -1;
for (int i = 0; i < n; i++)
{
ans = max (ans, solve(i));
}
cout << ans;
return 0;
}
What edge case am I missing here?