IzhanVarsky

Untitled

Mar 20th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. long long matches = 0;
  9. vector<map<char, vector<int>>> wordsByMap;
  10. string words[4];
  11.  
  12. void checkCase(int a, int b, int c, int d) {
  13.     string top = words[a];
  14.     string bot = words[b];
  15.     string left = words[c];
  16.     string right = words[d];
  17.     map<char, vector<int>> mapTop = wordsByMap[a];
  18.     map<char, vector<int>> mapBot = wordsByMap[b];
  19.     map<char, vector<int>> mapLeft = wordsByMap[c];
  20.     map<char, vector<int>> mapRight = wordsByMap[d];
  21.     vector<int> xCoordTop;
  22.     vector<int> yCoordTop;
  23.     for (auto &it1 : mapTop) {
  24.         char topAt = it1.first;
  25.         if (mapLeft.count(topAt) != 0) {
  26.             auto it2 = mapLeft.find(topAt);
  27.             for (int i : it1.second) {
  28.                 xCoordTop.push_back(i);
  29.             }
  30.             for (int i : it2->second) {
  31.                 yCoordTop.push_back(i);
  32.             }
  33.         }
  34.     }
  35.     vector<int> xCoordBot;
  36.     vector<int> yCoordBot;
  37.     for (auto &it1 : mapBot) {
  38.         char botAt = it1.first;
  39.         if (mapRight.count(botAt) != 0) {
  40.             auto it2 = mapRight.find(botAt);
  41.             for (int i : it1.second) {
  42.                 xCoordBot.push_back(i);
  43.             }
  44.             for (int i : it2->second) {
  45.                 yCoordBot.push_back(i);
  46.             }
  47.         }
  48.     }
  49.     for (unsigned int i = 0; i < xCoordTop.size(); ++i) {
  50.         for (unsigned int j = 0; j < xCoordBot.size(); ++j) {
  51. // Теперь, когда получили пару уголков
  52.             for (auto k = static_cast<unsigned char>(xCoordTop[i] + 1); k < top.size(); ++k) {
  53.                 for (auto t = static_cast<unsigned char>(yCoordTop[i] + 1); t < left.size(); ++t) {
  54. // Теперь мы имеем координаты кокретной точки, которая
  55. // ниже верхнего слова и правее левого
  56.                     if ((xCoordBot[j] != 0 && yCoordBot[j] != 0) &&
  57.                         (((xCoordBot[j] + xCoordTop[i]) >= k)) &&
  58.                         (((yCoordBot[j] + yCoordTop[i]) >= t))) {
  59.                         if ((left.at(t) == bot.at(xCoordBot[j] + xCoordTop[i] - k))
  60.                             && (top.at(k) == right.at(yCoordBot[j] + yCoordTop[i] - t))) {
  61.                             matches += 2;
  62.                         }
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }
  69.  
  70. int main() {
  71.     ifstream in("crosswords.in");
  72.     ofstream out("crosswords.out");
  73.     for (auto &word : words) {
  74.         in >> word;
  75.         map<char, vector<int>> tmp;
  76.         for (int j = 0; j < word.length(); ++j) {
  77.             tmp[word[j]].push_back(j);
  78.         }
  79.         wordsByMap.push_back(tmp);
  80.     }
  81.     // top bot left right
  82.     // int olymp ifmo ramp
  83.     // 0 3 1 2
  84.     // ifmo ramp int olymp
  85.     // 1 2 0 3
  86.     checkCase(0, 1, 2, 3);
  87.     checkCase(0, 1, 3, 2);
  88.     checkCase(1, 0, 2, 3);
  89.     checkCase(1, 0, 3, 2);
  90.  
  91.     checkCase(0, 2, 1, 3);
  92.     checkCase(0, 2, 3, 1);
  93.     checkCase(2, 0, 1, 3);
  94.     checkCase(2, 0, 3, 1);
  95.  
  96.     checkCase(0, 3, 1, 2);
  97.     checkCase(0, 3, 2, 1);
  98.     checkCase(3, 0, 1, 2);
  99.     checkCase(3, 0, 2, 1);
  100.     out << matches;
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment