title: Words of length k generation from a DFA
body: <p>Good day,
  I'm having a serious problem into generating a list with all the words of length k (the generate function is the function intended to generate all the words of length k, the other function is used to find out if a word is accepted or not) from a DFA just by using the DFS algorithm, here's my attempt:</p>
  
  <pre><code>#include&lt;vector&gt;
  #include&lt;iostream&gt;
  #include&lt;fstream&gt;
  #include&lt;string.h&gt;
  
  using namespace std;
  
  vector &lt; pair &lt;int,char&gt; &gt; a[100];
  
  int viz[100], v[100];
  char s1[100];
  
  void accepted (int q, char c, char s[100], int x) {
      c = s[x];
      viz[q] = 1;
      cout &lt;&lt; q;
      for (int i = 0; i &lt; a[q].size(); i++)
          if (a[q][i].second == c) {
              x++;
              accepted (a[q][i].first, a[q][i+1].second, s, x);
          }
  }
  
  void generate (int q, int k) {
      int x = 0;
      v[q] = 1;
      while (x &lt; k) {
          cout &lt;&lt; a[q][0].second;
          for (int i = 0; i &lt; a[q].size(); i++)
              if (v[a[q][i].first] == 0)
              {
                  cout &lt;&lt; a[q][i].second;
                  generate(a[q][i].first, k);
              }
          x++;
      }
  }
  
  int main() {
      ifstream f ("input.txt");
      int n, m, x, y, i, j, k;
      char c;
      char s[100];
      f &gt;&gt; n &gt;&gt; m;
      for (i = 0; i &lt; m; i++) {
              f &gt;&gt; x &gt;&gt; y;
              f &gt;&gt; c;
              a[x].push_back (make_pair(y,c));
              }
      for (i = 0; i &lt; n; i++) {
          cout &lt;&lt; i &lt;&lt; ": ";
          for (j = 0; j &lt; a[i].size(); j++)
              cout &lt;&lt; a[i][j].first &lt;&lt; a[i][j].second &lt;&lt; " ";
          cout &lt;&lt; endl;
      }
      cout &lt;&lt; endl &lt;&lt; "Fiite states: 2, 3" &lt;&lt; endl &lt;&lt; endl;
      cin.get (s,100);
      accepted(0, s[0], s, 0);
      if (viz[2] == 1) cout &lt;&lt; endl &lt;&lt; "Accepted";
      cout &lt;&lt; endl;
      cin &gt;&gt; k;
      generate (0, k);
      cout &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>Also here's how my input looks like:</p>
  
  <pre><code>4 6
  0 0 a
  0 1 b
  1 2 c
  2 2 a
  2 3 c
  3 3 c
  </code></pre>
  
  <p>Here's how the DFA and the output would look like:
  <a href="https://i.stack.imgur.com/9M4rj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9M4rj.png" alt="graph photo"></a></p>
  
  <p>The serious problem I'm facing is that I can't find a way to ouput properly all the obtained words to the screen by calling the generate function.</p>