Advertisement
purxiz

Common Phrases Loop

Nov 24th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. int main () {
  8.   ifstream f;
  9.   f.open ("nospace2.txt");
  10.   struct data{
  11.     char *digits;
  12.     int repeats;
  13.   };
  14.   for(int w = 9; w < 3201; w = w + 8){
  15.   char *b;
  16.   b = new char[w];
  17.   b[(sizeof(b)/sizeof(*b))] = 0;
  18.  
  19.   vector<data> combo;
  20.   int count = 0;
  21.   while(f.read(b, w - 1)){
  22.     int found = 0;
  23.     //printf("%s  ", b);
  24.     for(int j = 0; j < combo.size(); j++){
  25.       //printf("comparing b = %s and digits = %s ", b, combo[j].digits);
  26.       if (strcmp(b, combo[j].digits) == 0) {
  27.           combo[j].repeats++;
  28.           found = 1;
  29.           //printf("They match!");
  30.       }
  31.       //printf("\n");
  32.     }
  33.     if(found == 0){
  34.       combo.push_back(data());
  35.       combo[count].digits = new char[w];
  36.       strcpy(combo[count].digits, b);
  37.       combo[count++].repeats = 1;
  38.       //printf("no match was found, setting combo[%d].repeats = %d and digits = %s\n", count - 1, combo[count - 1].repeats, combo[count - 1].digits); //ignore
  39.     }
  40.   //  f.seekg((-w+9), f.cur);
  41.   }
  42.   f.clear();
  43.   f.seekg(0);
  44.   int numoftwos = 0;
  45.   int pos = 0;
  46.   for(int k = 0; k < combo.size(); k++){
  47.     if(combo[k].repeats == 2){
  48.       numoftwos++;
  49.       pos = k;
  50.     }
  51.   }
  52.   if(numoftwos == 1){
  53.     printf("It worked with n = %d, value is %s\n", w, combo[pos].digits);
  54.     //break;
  55.   }else{
  56.     printf("Failed for n = %d, numoftwos = %d\n", w, numoftwos);
  57.   }
  58.   delete [] b;
  59.   combo.clear();
  60.   vector<data>().swap(combo);
  61. }
  62. /*
  63.   vector<int> max;
  64.   vector<int> pos;
  65.  
  66.   for (int k = 0; k < combo.size(); k++) {
  67.     max.push_back(-5000000);
  68.     pos.push_back(0);
  69.     for(int i = 0; i < combo.size(); i++){
  70.       //printf("%d\n", combo[i][0]); //ignore
  71.       if(combo[i].repeats > max[k]){
  72.         max[k] = combo[i].repeats;
  73.         pos[k] = i;
  74.       }
  75.     }
  76.     printf("%s occured %d times\n", combo[pos[k]].digits, max[k]);
  77.     combo[pos[k]].repeats = -1;
  78.   }
  79. */
  80.   f.close();
  81.   return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement