fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define LEN 128
  6. #define MAX 128
  7. #define MAXLETTER 6
  8.  
  9. int main(void) {
  10. struct {
  11. int upcount;
  12. int downcount;
  13. int up[100];
  14. int down[100];
  15. } alphabet[MAXLETTER];
  16. for(int i=0;i<MAXLETTER;i++)
  17. alphabet[i].upcount=alphabet[i].downcount=0;
  18. char line[LEN];
  19. while(fgets(line,LEN,stdin)!=NULL)
  20. {
  21. int len=strlen(line);
  22. if(line[len-1]=='\n')
  23. line[--len]=0;
  24. char aa, bb;
  25. sscanf(line, "Step %c must be finished before step %c can begin.", &aa, &bb);
  26. int a=aa-'A';
  27. int b=bb-'A';
  28.  
  29. alphabet[a].down[alphabet[a].downcount++] = b;
  30. alphabet[b].up[alphabet[b].upcount++] = a;
  31. }
  32.  
  33. int top;
  34. for(int i=0;i<MAXLETTER;i++)
  35. if(alphabet[i].upcount==0)
  36. top=i;
  37. printf("%d\n",top);
  38. int bottom;
  39. for(int i=0;i<MAXLETTER;i++)
  40. if(alphabet[i].downcount==0)
  41. bottom=i;
  42. int available[MAXLETTER];
  43. for(int i=0;i<MAXLETTER;i++)
  44. available[i]=0;
  45.  
  46. exit(0);
  47.  
  48. int solution[100];
  49. int k=0;
  50. int current=top;
  51.  
  52. int flag=0;
  53. while(1)
  54. {
  55. for(int i=0;i<alphabet[current].downcount;i++)
  56. available[alphabet[current].down[i]]=1;
  57.  
  58. for(int i=0;i<MAXLETTER;i++)
  59. {
  60. if(available[i])
  61. {
  62. solution[k++]=i;
  63. current=i;
  64. available[i]=0;
  65. break;
  66. }
  67. }
  68.  
  69.  
  70. }
  71.  
  72. solution[k]=0;
  73. for(int i=0;i<k;i++)
  74. printf("%c",solution[i]+'A');
  75. return 0;
  76. }
  77.  
Success #stdin #stdout 0.01s 5292KB
stdin
Step C must be finished before step A can begin.
Step C must be finished before step F can begin.
Step A must be finished before step B can begin.
Step A must be finished before step D can begin.
Step B must be finished before step E can begin.
Step D must be finished before step E can begin.
Step F must be finished before step E can begin.
stdout
2