Advertisement
Guest User

Untitled

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