fork(5) download
  1. #include <bits/stdc++.h>
  2.  
  3. #define INF -10000000
  4. #define FACTOR 10
  5.  
  6. using namespace std;
  7.  
  8. double memo[100][100][100];
  9.  
  10. double Levenshtein(string inputWord, string checkWord, int i, int j, int count){
  11. if(i == inputWord.length() && j == checkWord.length()) return 0;
  12. if(i == inputWord.length()) return checkWord.length() - j;
  13. if(j == checkWord.length()) return inputWord.length() - i;
  14. if(memo[i][j][count] != INF) return memo[i][j][count];
  15.  
  16. double ans1 = 0, ans2 = 0, ans3 = 0, ans = 0;
  17. if(inputWord[i] == checkWord[j]){
  18. ans1 = Levenshtein(inputWord, checkWord, i+1, j+1, count+1) - (FACTOR*(count+1));
  19. ans2 = Levenshtein(inputWord, checkWord, i+1, j, 0) + 1;
  20. ans3 = Levenshtein(inputWord, checkWord, i, j+1, 0) + 1;
  21. ans = min(ans1, min(ans2, ans3));
  22. }else{
  23. ans1 = Levenshtein(inputWord, checkWord, i+1, j, 0) + 1;
  24. ans2 = Levenshtein(inputWord, checkWord, i, j+1, 0) + 1;
  25. ans = min(ans1, ans2);
  26. }
  27. return memo[i][j][count] = ans;
  28. }
  29.  
  30. int main(void) {
  31. // your code goes here
  32. string word = "job";
  33. string wordList[40];
  34. vector< pair <double, string> > ans;
  35. for(int i = 0;i < 40;i++){
  36. cin >> wordList[i];
  37. for(int j = 0;j < 100;j++) for(int k = 0;k < 100;k++){
  38. for(int m = 0;m < 100;m++) memo[j][k][m] = INF;
  39. }
  40. ans.push_back( make_pair(Levenshtein(word, wordList[i],
  41. 0, 0, 0), wordList[i]) );
  42. }
  43. sort(ans.begin(), ans.end());
  44. for(int i = 0;i < ans.size();i++){
  45. cout << ans[i].second << " " << ans[i].first << endl;
  46. }
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0.02s 11296KB
stdin
joborienterede
jobannoncer
jobfunktioner
perjacobsen
jakobsen
jobprofiler
jacob
jobtitler
jobbet
jobdatabaserne
jobfunktion
jakob
jobs
studenterjobber
johannesburg
jobmuligheder
jobannoncerne
jobbaser
job
joberfaringer
Hammer
Hat
Hieroglyph
Highway
Horoscope
Horse
Hose
Ice
Ice-cream
Insect
Jet fighter
Junk
Kaleidoscope
Kitchen
Knife
Leather jacket
Leg
Library
Liquid
Magnet
stdout
job -60
jobs -59
jobbet -57
jobbaser -55
jobtitler -54
jobannoncer -52
jobfunktion -52
jobprofiler -52
jobannoncerne -50
joberfaringer -50
jobfunktioner -50
jobmuligheder -50
jobdatabaserne -49
joborienterede -49
studenterjobber -48
jacob -38
jakob -38
jakobsen -35
perjacobsen -32
johannesburg -31
Hose -5
Horse -4
jacket -3
Library -2
Horoscope 0
Hieroglyph 1
Kaleidoscope 3
Hat 6
Ice 6
Jet 6
Leg 6
Junk 7
Knife 8
Hammer 9
Insect 9
Highway 10
Kitchen 10
Leather 10
fighter 10
Ice-cream 12