Advertisement
osipyonok

ppl1

Apr 24th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. // String_sequential.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <ctime>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13. int main(){
  14.     FILE * file;
  15.     file = fopen("input_big.txt", "r");
  16. //  file = fopen("input_stupid.txt", "r");
  17. //  file = fopen("input_no_ans.txt", "r");
  18.     int n;
  19.  
  20.     fscanf(file, "%d", &n);
  21.     vector<string> v(n);
  22.  
  23.     for (int i = 0; i < n; ++i) {
  24.         char tmp[10000];
  25.         fscanf(file, "%s", &tmp);
  26.         v[i] = string(tmp);
  27.     }
  28.     fclose(file);
  29.  
  30.     long long start = clock();
  31.  
  32.     int ans = 1e7;
  33.  
  34.     for (int i = 0; i < n; ++i) {
  35.         int cur = 0;
  36.         for (int j = 0; j < n; ++j) {
  37.             string tmp = v[j] + v[j];
  38.             size_t p = tmp.find(v[i]);
  39.             if (p == string::npos) {
  40.                 ans = -1;
  41.             }
  42.             else
  43.             {
  44.                 cur += p;
  45.             }
  46.         }
  47.         ans = min(ans, cur);
  48.     }
  49.  
  50.     cout << ans << endl;
  51.  
  52.     long long end = clock();
  53.  
  54.     cout << (double)(end - start) / CLOCKS_PER_SEC << " sec " << endl;
  55.  
  56.     system("pause");
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement