fork(2) download
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <algorithm>
  4. #include <numeric>
  5. #include <functional>
  6.  
  7. int min_index(const std::vector<int>& list)
  8. {
  9. int index = 0;
  10. int smallest = list[0];
  11.  
  12. for (size_t i = 0; i < list.size(); ++i) {
  13. if (list[i] < smallest) {
  14. smallest = list[i];
  15. index = i;
  16. }
  17. }
  18. return index;
  19. }
  20.  
  21. std::uint32_t LevenshteinDistance(const std::string &First, const std::string &Second)
  22. {
  23. const std::size_t FirstLength = First.size();
  24. const std::size_t SecondLength = Second.size();
  25.  
  26. std::vector<std::uint32_t> Current(SecondLength + 1);
  27. std::vector<std::uint32_t> Previous(SecondLength + 1);
  28.  
  29. std::size_t I = 0;
  30. std::generate(Previous.begin(), Previous.end(), [&] {return I++; });
  31.  
  32. for (I = 0; I < FirstLength; ++I)
  33. {
  34. Current[0] = I + 1;
  35.  
  36. for (std::size_t J = 0; J < SecondLength; ++J)
  37. {
  38. auto Cost = First[I] == Second[J] ? 0 : 1;
  39. Current[J + 1] = std::min(std::min(Current[J] + 1, Previous[J + 1] + 1), Previous[J] + Cost);
  40. }
  41.  
  42. Current.swap(Previous);
  43. }
  44. return Previous[SecondLength];
  45. }
  46.  
  47. std::vector<std::string> questions =
  48. {
  49. "What is the most popular program at GBC?",
  50. "How much is the tuition at GBC?",
  51. "Do I have to pay my fees before I can register?",
  52. "What are my fee payment options?",
  53. "How do I know when I'm allowed to register?",
  54. "How do I add and/or drop courses from my timetable?",
  55. "What do I do if I can't find my PASSWORD?",
  56. "How do I withdraw from a program?",
  57. "What are the college policies?",
  58. "How much math do I need to know?",
  59. "What is the program code for computer programming?",
  60. "What is stu-view?",
  61. "What is the college best known for?",
  62. "How easy is it to find work after program completion?",
  63. "What if I plan to continue my education after?"
  64. };
  65.  
  66. std::vector<std::string> answers =
  67. {
  68. "Fashion",
  69. "3000 a semester",
  70. "Yes you have to pay the fees before registering",
  71. "You may pay online on your student account through the student portal",
  72. "You may register two weeks or more before the start of the program",
  73. "You may drop courses from online through the student portal",
  74. "You can call ... and an agent will assist you",
  75. "You may withdraw using the student portal online",
  76. "They are located at the following link...",
  77. "It depends on the program you are entering",
  78. "T127 is the code for computer science",
  79. "Stu-View is a student portal to manage student account and view marks.",
  80. "The cafeteria food",
  81. "Depends on the field of work and timing",
  82. "You may do so within three years after program completion"
  83. };
  84.  
  85. int main()
  86. {
  87. std::string user_question = "program";
  88.  
  89. std::vector<int> distances = std::vector<int>(questions.size(), 0);
  90.  
  91. for (size_t I = 0; I < questions.size(); ++I)
  92. {
  93. int dist = LevenshteinDistance(user_question, questions[I]);
  94. distances[I] = dist;
  95. }
  96.  
  97. std::cout<<"Distance: "<<distances[min_index(distances)]<<"\n";
  98. std::cout<<"User-Question: "<<user_question<<"\n";
  99. std::cout<<"Question-Key: "<<questions[min_index(distances)]<<"\n";
  100. std::cout<<"Answer-Value: "<<answers[min_index(distances)]<<"\n";
  101.  
  102. return 0;
  103. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Distance:      17
User-Question: program
Question-Key:  What is stu-view?
Answer-Value:  Stu-View is a student portal to manage student account and view marks.