fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <iterator>
  5. #include <sstream>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. vector<int> str2vec (const string& s) {
  10. string s1{s};
  11. replace_if (s1.begin(), s1.end(), [](char c) { return c == ';'; }, ' ');
  12. vector<int> v;
  13. stringstream ss(s1);
  14. copy (istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(v));
  15.  
  16. return v;
  17. }
  18.  
  19. typedef vector<int>::const_iterator iter;
  20.  
  21. iter approximate_find (const vector<int>& needle, const vector<int>& input, double prec=0) {
  22. iter i=input.cbegin();
  23. while (i < input.cend()-needle.size()) {
  24. bool found = true;
  25. for (iter j=needle.cbegin(); j != needle.cend(); ++j, ++i)
  26. if (fabs (*i-*j) > *i*prec) {
  27. found = false;
  28. ++i;
  29. break;
  30. }
  31. if (found)
  32. return i-needle.size();
  33. }
  34. return input.cend();
  35. }
  36.  
  37. int main() {
  38. string input { "100;1000;10000;100000;900;75;871;347;" };
  39. vector<string> dictionary {
  40. "77;15;224;34;781;574;1561;"
  41. ,"800;8000;80000;850;"
  42. ,"13;16;61;516;4;864;61;98;46;8189;63;"
  43. };
  44.  
  45. vector<vector<int>> v_dic;
  46. transform (dictionary.begin(), dictionary.end(), back_inserter(v_dic), str2vec);
  47. vector<int> v_input = str2vec(input);
  48.  
  49. for_each (v_dic.begin(), v_dic.end(), [&](const vector<int>& needle) {
  50. iter i = approximate_find (needle, v_input, 0.2);
  51. if (i != v_input.cend()) {
  52. copy (i, i+needle.size(), ostream_iterator<int>(cout, ";"));
  53. cout << " matches ";
  54. }
  55. else {
  56. cout << "not found: ";
  57. }
  58. copy (needle.begin(), needle.end(), ostream_iterator<int>(cout, ";"));
  59. cout << endl;
  60. });
  61. }
  62.  
Success #stdin #stdout 0s 3280KB
stdin
Standard input is empty
stdout
not found: 77;15;224;34;781;574;1561;
1000;10000;100000;900; matches 800;8000;80000;850;
not found: 13;16;61;516;4;864;61;98;46;8189;63;