Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5. #define ll long long
  6.  
  7. // int a[26][26] = {{NULL}};
  8.  
  9. bool cflags[26];
  10.  
  11. vector<map<char, int>> columns;
  12. vector<char> acids;
  13. vector<vector<long double>> matrix;
  14. vector<vector<long double>> S;
  15.  
  16. vector<string> data;
  17.  
  18. void strip(std::string &str ){
  19.     if  (!(str.length() == 0)) {
  20.         auto w = std::string(" ") ;
  21.         auto n = std::string("\n") ;
  22.         auto r = std::string("\t") ;
  23.         auto t = std::string("\r") ;
  24.         auto v = std::string(1 ,str.front());
  25.         while((v == w) or(v==t)or(v==r)or(v==n)) {str.erase(str.begin()) ;v = std::string(1 ,str.front()); }
  26.         v = std::string(1 , str.back()) ;
  27.         while((v ==w) or(v==t)or(v==r)or(v==n)) {str.erase(str.end() - 1 )  ;v = std::string(1 , str.back()) ;}
  28.     }
  29. }
  30.  
  31. void inc_columns(const string& s) {
  32.     for (int i = 0; i < s.size(); i++) {
  33.         columns[i][s[i]]++;
  34.         if (!cflags[s[i] - 'A']) {
  35.             cflags[s[i] - 'A'] = true;
  36.             acids.push_back(s[i]);
  37.         }
  38.     }
  39. }
  40.  
  41. int main() {
  42.     //ios::sync_with_stdio(0);
  43.     //cin.tie(NULL);
  44.     fesetround(FE_TONEAREST);
  45.     ifstream in("input.txt");
  46.     freopen("output.txt", "w", stdout);
  47.  
  48.     string s;
  49.  
  50.     in >> s;
  51.  
  52.     columns.resize(s.size());
  53.     inc_columns(s);
  54.  
  55.     int k = 1;
  56.  
  57.     while (in >> s) {
  58.         strip(s);
  59.         if (s.size() > 0) {
  60.             k++;
  61.             inc_columns(s);
  62.         }
  63.     }
  64.  
  65.     if (k == 1) {
  66.         return 0;
  67.     }
  68.  
  69.     int t = s.size() * k * (k - 1) / 2;
  70.  
  71.     sort(acids.begin(), acids.end());
  72.     matrix.resize(acids.size(), vector<long double>(acids.size(), 0));
  73.  
  74.     for (int i = 0; i < acids.size(); i++) {
  75.         for (int j = 0; j < acids.size(); j++) {
  76.             int pairs = 0;
  77.             for (auto& c: columns) {
  78.                 if (c.find(acids[i]) == c.end() or c.find(acids[j]) == c.end()) { continue; }
  79.                 if (i == j) {
  80.                     pairs += abs((int)(c[acids[i]] * (c[acids[i]] - 1)) / 2);
  81.                 } else {
  82.                     pairs += abs((int)(c[acids[i]] * c[acids[j]]));
  83.                 }
  84.             }
  85.             if (pairs == 0) {
  86.                 matrix[i][j] = abs((long double)pairs);
  87.             } else {
  88.                 matrix[i][j] = abs((long double)pairs / (long double)t);
  89.             }
  90.         }
  91.     }
  92.  
  93.     vector<long double> P(acids.size(), 0);
  94.     for (int i = 0; i < acids.size(); i++) {
  95.         for (int j = 0; j < acids.size(); j++) {
  96.             if (i != j) {
  97.                 P[i] += matrix[j][i];
  98.             }
  99.         }
  100.         P[i] = P[i] / 2.0 + matrix[i][i];
  101.     }
  102.  
  103.     S.resize(acids.size(), vector<long double>(acids.size(), 0));
  104.  
  105.     for (int i = 0; i < acids.size(); i++) {
  106.         for (int j = 0; j < acids.size(); j++) {
  107.             long double e = P[i] * P[j];
  108.             if (i != j) {
  109.                 e *= 2.0;
  110.             }
  111.             if (matrix[i][j] != 0) {
  112.                 S[i][j] = nearbyint(2.0 * log2(matrix[i][j] / e));
  113.             }
  114.         }
  115.     }
  116.  
  117.     for (int i = 0; i < S.size(); i++) {
  118.         for (int j = 0; j < S[i].size(); j++) {
  119.             if (j <= i) {
  120.                 if (S[i][j] == 0) {
  121.                     S[i][j] = abs(S[i][j]);
  122.                 }
  123.                 cout << S[i][j] << " ";
  124.             }
  125.         }
  126.         cout << endl;
  127.     }
  128.  
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement