无法弄清楚我的 UVA 10029 代码的问题

问题描述

问题的链接here

问题要求编辑阶梯。编辑阶梯是一系列转换,其中编辑距离为 1,单词按字典顺序递增。根据onlinejudge board的想法,我实现了一个解决方案。首先生成一个无向图,其中每条边都意味着一个有效的转换/编辑步骤。然后使用 DFS 找到最大深度。但是我在以下测试用例中得到了错误的答案。

aaaaaa
aaaaaaa
aaaaaah
aaaaah
aaaah
aaah
aaarg
aaarge
aaarh
aabrge
aacrd
aacre
aaraah
aard
araah
ard
arde
arded
ardev
raah
rdev
vaah
vaahb
vaahe

答案应该是 11。但我的代码目前返回 8。代码如下所示。我正在做的唯一不同的步骤是在最后添加一个 $。这样做是为了避免我写两个循环。我有一个循环,我在当前字符之前添加字符,并且通过使用美元符号的虚拟字符,它也可以用于在字符串的末尾添加字符。我已经用单独的编辑步骤程序做了一些检查,生成的图形似乎是正确的。我很可能在 DFS 部分犯了一个错误。具体来说,这一行

if (!discovered[y] and ind_to_str[v] < ind_to_str[y])

对此的任何帮助表示赞赏。

#include <bits/stdc++.h>
#define MAX (25000 + 2)

using namespace std;

int max_depth = 0;
int dtime = 0;
vector<unordered_set<int>> graph;
vector<string> words;
unordered_map<string,int> str_to_ind;
unordered_map<int,string> ind_to_str;
vector<int> cur_chain,best_chain;

int parent[MAX];
int discovered[MAX];
int processed[MAX];
int entry_time[MAX];
int exit_time[MAX];

void dfs(int v,int d) {
  cout << "At " << ind_to_str[v] << " with depth " << d << endl;
  // max_depth = max(max_depth,d);
  cur_chain.push_back(v);
  if (d > max_depth) {
    max_depth = d;
    best_chain = cur_chain;
  }
  discovered[v] = true;
  entry_time[v] = ++dtime;

  for (int y : graph[v]) {
    if (!discovered[y] and ind_to_str[v] < ind_to_str[y]) {
    // if (!discovered[y]) {
      parent[y] = v;
      dfs(y,d + 1);
    }
  }

  exit_time[v] = ++dtime;
  processed[v] = true;
  cur_chain.pop_back();
}

void init_search() {
  for (int i = 0; i < MAX; ++i) {
    parent[i] = entry_time[i] = exit_time[i] = -1;
    discovered[i] = processed[i] = false;
  }
  dtime = 0;
}

int main(void) {
  string line;
  int ind = 0;
  while (getline(cin,line) and line.length() > 0) {
    // append an extra dollar so that you don't have to
    // have an extra loop at the end for adding characters
    // at the end of a string,you can have one loop which
    // adds characters before every character and you've
    // added characters in all places
    line += "$";
    words.push_back(line);
    str_to_ind[line] = ind;
    ind_to_str[ind++] = line;
  }
  int n = words.size();
  
  graph.assign(n,unordered_set<int>());

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < words[i].length() - 1; ++j) {
      for (char c = 'a'; c <= 'z'; ++c) {
        // insert
        string added = words[i].substr(0,j) + string(1,c) + words[i].substr(j + 1,string::npos);
        if (str_to_ind.count(added) != 0) {
          int cur_ind = str_to_ind[words[i]];
          int next_ind = str_to_ind[added];
          if (cur_ind == next_ind) continue;
          
          graph[cur_ind].insert(next_ind);
          graph[next_ind].insert(cur_ind);
        }

        // replace
        string changed = words[i];
        changed[j] = c;
        if (str_to_ind.count(changed) != 0) {
          int cur_ind = str_to_ind[words[i]];
          int next_ind = str_to_ind[changed];
          if (cur_ind == next_ind) continue;

          graph[cur_ind].insert(next_ind);
          graph[next_ind].insert(cur_ind);
        }
      }

      // delete
      string deleted = words[i].substr(0,j) + words[i].substr(j + 1,string::npos);
      if (str_to_ind.count(deleted) != 0) {
        int cur_ind = str_to_ind[words[i]];
        int next_ind = str_to_ind[deleted];
        if (cur_ind == next_ind) continue;

        graph[cur_ind].insert(next_ind);
        graph[next_ind].insert(cur_ind);
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << ind_to_str[i] << ": ";
    for (int x : graph[i]) {
      cout << ind_to_str[x] << " ";
    }
    printf("\n");
  }

  for (int i = 0; i < n; ++i) {
    cur_chain.clear();
    init_search();
    cout << "Starting with " << ind_to_str[i] << endl;
    dfs(i,1);
  }

  printf("%d\n",max_depth);
  for (int i = 0; i < best_chain.size(); ++i) {
    cout << ind_to_str[best_chain[i]] << endl;
  }
  
  return 0;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)