Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define el endl
- #define umi unordered_map<int, int>
- #define umll unordered_map<ll, ll>
- #define all(vect) vect.begin(), vect.end()
- #define reset(A) memset(A, true, sizeof(A))
- #define approx(n) fixed << setprecision(n)
- #define pb push_back
- const int mod = 1e9 + 7;
- using namespace std;
- int n, a[15];
- string s[15];
- bool check[15];
- int minn = INT_MAX;
- int ok()
- {
- int res = 0;
- for(int i = 1; i < n; i++)
- {
- for(int j = 0; j < s[a[i]].size(); j++)
- {
- for(int k = 0; k < s[a[i + 1]].size(); k++)
- {
- if(s[a[i]][j] == s[a[i + 1]][k])
- res++;
- }
- }
- }
- return res;
- }
- void back(int i)
- {
- for(int j = 1; j <= n; j++)
- {
- if(check[j])
- {
- a[i] = j;
- check[j] = false;
- if(i == n)
- {
- minn = min(minn, ok());
- // for(int i = 1; i <= n; i++)
- // {
- // cout << a[i] << " ";
- // }
- // cout << el << el;
- // cout << minn << el;
- }
- else
- back(i + 1);
- check[j] = true;
- }
- }
- }
- void solve()
- {
- reset(check);
- cin >> n;
- minn = 100000;
- for(int i = 1; i <= n; i++)
- {
- cin >> s[i];
- }
- back(1);
- cout << minn << el;
- }
- int main()
- {
- int t = 1;
- // cin >> t;
- // cin.ignore();
- while(t--)
- {
- solve();
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment