szabivan

(a?)^na^n matching to a^n, cpp

Feb 2nd, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <regex>
  4. #include <iostream>
  5. #include <ctime>
  6.  
  7. bool match_an(int n){
  8.     char *theRegexString = new char[3 * n + 1];
  9.     char *theText = new char[n + 1];
  10.     char* ptr = theRegexString;
  11.     for (int i = 0; i < n; i++){
  12.         *(ptr++) = 'a';
  13.         *(ptr++) = '?';
  14.     }
  15.     for (int i = 0; i < n; i++) {
  16.         *(ptr++) = 'a'; theText[i] = 'a';
  17.     }
  18.     *ptr = 0; theText[n] = 0;
  19.  
  20.     clock_t t = clock();
  21.  
  22.     std::regex theRegex(theRegexString,std::regex::optimize|std::regex::ECMAScript);
  23.     std::smatch theMatch;
  24.     std::string theTextString(theText);
  25.     bool ret = std::regex_match(theTextString, theMatch, theRegex);
  26.     t = clock() - t;
  27.     std::cout << "Matching (a?)^na^n on a^n for n = " << n << " we get a " << ret << " in " << ((float)t) / CLOCKS_PER_SEC << " seconds." << std::endl;
  28.     return ret;
  29. }
  30.  
  31. int _tmain(int argc, _TCHAR* argv[])
  32. {
  33.     for (int i = 1; i < 30; i++){
  34.         match_an(i);
  35.     }
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment