798B - Mike and strings - CodeForces Solution


brute force dp strings *1300

Please click on ads to support us..

C++ Code:

// لله درك
#pragma GCC optimize ("O3")
#pragma GCC optimization("Ofast")
#pragma GCC target("avx2")
#pragma GCC optimization("unroll-loops")
#include <bits/extc++.h>
#define Caraxes  ios_base :: sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define int long long int
#define F first
#define S second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define inf 1e18
using namespace std;

template< typename  T = int >
std :: vector < T > get(int size = 0, std :: istream& in_stream = std :: cin);
int calc(string a, string b){
    if(a == b)
        return 0;
    if(sz(a) != sz(b))
        return -1;
    int cnt = 0;
    string tmp1 = a, tmp2 = b;
    sort(all(a));
    sort(all(b));
    for(int i = 0;i < sz(a); ++i){
        if(a[i] != b[i])
            return -1;
    }
    a = tmp1;
    b = tmp2;
    while(b != a){
        b.push_back(b.front());
        b.erase(b.begin());
        cnt++;
        if(cnt && b == tmp2)
            return -1;
    }
    return cnt;
}
void Dracarys() {
    int n;
    cin >> n;
    vector < string > v(n);
    for(int i = 0;i < n; ++i){
        cin >> v[i];
    }
    int ans = INT_MAX;
    for(int i = 0;i < n; ++i){
        int cnt = 0;
        for(int j = 0;j < n; ++j){
            int z = calc(v[i],v[j]);
            if(z == -1){
                return cout << -1,void();
            }
            cnt+= z;
        }
        ans = min(ans,cnt);
    }
    cout << ans << '\n';
}

signed main()
{
    Caraxes
     #ifndef ONLINE_JUDGE
      ::freopen("in.txt","r",stdin);
      ::freopen("out.txt","w",stdout);
    #endif
    int test = 1;
    //cin >> test;
    while(test--) {
        Dracarys();
    }
}

template< typename  T>
std :: vector < T > get(int size , std :: istream& in_stream){
    if(!size){
        in_stream >> size;
    }
    std :: vector < T > array(size);
    for(auto& i : array)
        in_stream >> i;
    return array;
}


Comments

Submit
0 Comments
More Questions

415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function