답안 #366558

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
366558 2021-02-14T13:54:19 Z Atill83 Vještica (COCI16_vjestica) C++14
160 / 160
108 ms 5100 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 1e5+5;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n;
string s[MAXN];
int cnt[MAXN][26];
int dp[MAXN];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);

    #ifdef Local
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
    #endif

    cin>>n;
    memset(dp, 0x7f, sizeof(dp));

    for(int i = 0; i < n; i++){
        cin>>s[i];
        for(char j: s[i])
            cnt[i][(j - 'a')]++;
        dp[(1<<i)] = s[i].length();
    }

    for(int i = 1; i < (1<<n); i++){
        int orti = 0;
        vector<int> kc(26, mod);

        for(int j = 0; j < n; j++){
            if((1<<j) & i)
                for(int l = 0; l < 26; l++)
                    kc[l] = min(kc[l], cnt[j][l]);
        }

        for(int l = 0; l < 26; l++){
            orti += kc[l];
        }


        for(int j = (i & (i - 1)); j; j = (i & (j - 1))){
            dp[i] = min(dp[i], dp[j] + dp[i ^ j] - orti);
        }
    }

    cout<<dp[(1<<n) - 1] + 1<<endl;



    

    #ifdef Local
        cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3820 KB Output is correct
2 Correct 3 ms 3820 KB Output is correct
3 Correct 3 ms 3820 KB Output is correct
4 Correct 98 ms 3948 KB Output is correct
5 Correct 104 ms 4076 KB Output is correct
6 Correct 108 ms 4588 KB Output is correct
7 Correct 103 ms 4972 KB Output is correct
8 Correct 100 ms 5100 KB Output is correct
9 Correct 103 ms 4972 KB Output is correct
10 Correct 101 ms 4844 KB Output is correct