Submission #64320661


Source Code Expand

/*
Written By : mafailure
In the name of God 
O Allah, May you grant peace and honor on Muhammad and his family.
Allahumm-a-Sall-iAla Muhammad-in Wa Al-i Muhammad
*/

#ifdef LOCAL 
#define AATIF_DEBUG
#endif 
/*Add -DLOCAL in 
compiler command 
to trigger it*/
  
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file 
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional> // for less
using namespace std;
using namespace __gnu_pbds;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 
#define endl "\n"
/*------------------------int to long long -----------------*/
#ifdef ill
#define int long long 
#endif
/*---------------------------DEBUG HELPER--------------------------------------*/
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T,typename K> ostream& operator<<(ostream & os,const map<T,K> & mapp){ os<<"{"; string sep=""; for(const auto& x:mapp)os<<sep<<x,sep=", "; return os<<'}'; }
template <typename T> ostream & operator<<(ostream & os,const set<T> & sett){os<<'{'; string sep=""; for(const auto & x:sett)os<<sep<<x,sep=", "; return os<<'}';}
 
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
 
#ifdef AATIF_DEBUG
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
 
//#define int long long 
// int dx[]={-1,1,0,0}; int dy[]={0,0,1,-1};
// int dx[]={2,2,-2,-2,1,1,-1,-1}; int dy[]={1,-1,1,-1,2,-2,2,-2};
#ifndef mod_2 
long long mod = 1e9 + 7;
#else 
long long mod =998244353; 
#endif 
const double eps=1e-9;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> ii;
typedef vector< pair< int, int > > vii;
typedef map<int, int> mii;
typedef pair<int, ii> pip;
typedef pair<ii, int> ppi;
#define arrinp(arr,init,final,size,type) type* arr=new type[size];for(int i=init;i<final;i++)cin>>arr[i];
#define cr2d(arr,n,m,t) t**arr=new t*[n];for(int i=0;i<n;i++)arr[i]=new t[m];
#define w(t) int t;cin>>t; while(t--)
#define takeInp(n) int n;cin>>n;
#define fr(i,init,final) for(int i=init;i<final;i++)
#define frr(i,init,final) for(int i=init;i>=final;i--)
#define Fr(i,final) for(int i=0;i<final;i++)
#define Frr(i,first) for(int i=first;i>=0;i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define debug(x) cerr<<">value ("<<#x<<") : "<<x<<endl;
#define setb __builtin_popcount
#define lsone(n) (n&(-n))
#define rlsone(n) (n&(n-1))
#define clr(a,b) memset(a,b,sizeof(a))
#ifdef ill 
const int inf =1e18; 
#else 
const int inf=1e9;
#endif
/*-----------------------------RANDOM NUMBER GENERATOR ---------------------*/
#ifdef RNG 
unsigned seed=chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(seed);
#endif 
/*------------------------------UNORDERED MAP HASH --------------------------------------------*/
//To make unordered_map unhackable 
// use it as unordered_map<int,int,custom_hash> mapp;
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        /* http://xorshift.di.unimi.it/splitmix64.c */
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
 
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
/*---------------------------ORDERED SET--------------------------------------*/
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>  ordered_set; 
/*----------------------------------------------------------------------------*/
vi init(string s)
{
    istringstream sin(s);
    int n;
    vi arr;
    while(sin>>n)arr.push_back(n);
    return arr;
}
int power(int x, int y)
{ 
    if(y==0)return 1;
    int u=power(x,y/2);
    u=(u*u)%mod;
    if(y%2)u=(x*u)%mod;
    return u;
    
}
int gcd(int a,int b)
{
    if(a<b)return gcd(b,a);
    return (b==0?a:(a%b?gcd(b,a%b):b));
}
int gcd_e(int a,int b,int &x,int &y)
{
    
    if(b==0){x=1; y=0; return a;}
    int x1,y1;
    int p=gcd_e(b,a%b,x1,y1);
    x=y1;
    y=x1-(a/b)*y1;
    return p;
}
/*-----------------to solve int to long long problem-----------------*/
int Min (int a,int b){return min(a,b);}
int Max(int a,int b){ return max(a,b);}
inline int add(int a,int b,int mod=mod){return (a+b)%mod;} 
inline int sub(int a,int b,int mod=mod){return (a-b+mod)%mod;}
inline int mul(int a,int b,int mod=mod){return (a*b%mod);}
inline int divide(int a,int b,int mod=mod){return a*power(b,mod-2)%mod;}
inline int high(int a,int b){return (a>>b)&1;}
//786 121 786 121 786 121 786 121 786 121 786 121 786 121 786 121 786 121
/*========================CODE*****CODE****CODE======================*/

vector<int> par; 
int gp(int u) {
    return par[u] == u? u: par[u]=gp(par[u]); 
}

bool join(int u,int v) {
    int p1 = gp(u),p2=gp(v);
    if(p1==p2)return false; 
    par[p1] = p2; 
    return true;
}


signed main()
{
    IOS
    int n; 
    string s,t; 
    cin>>n; 
    par = vi(26); 
    iota(all(par),0); 
    cin>>s>>t; 
    const int d = 26; 
    vvi g(26); 
    int cnt = 0; 
    fr(i,0,n){
        int u = s[i]-'a'; 
        int v = t[i] - 'a'; 
        if(g[u].empty() || (g[u].back()!=v)) 
        {
            g[u].push_back(v); 
            if(u!=v) 
            {
                bool x = join(u,v); 
                cnt+=!x; 
            }
        }
    }
    int ans = 0; 
    for(int i=0;i<d;i++){
        if(g[i].size()>1){
            cout<<-1<<endl; 
            return 0;
        }
        if(!g[i].empty()){
            ans+= (g[i].front() != i); 
        }
    }
    cout<<ans+cnt<<endl; 
    
         
}


Submission Info

Submission Time
Task E - Replace
User mafailure
Language C++ 20 (gcc 12.2)
Score 0
Code Size 6605 Byte
Status WA
Exec Time 2 ms
Memory 4088 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 4
AC × 48
WA × 39
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 02_random2_10.txt, 02_random2_11.txt, 02_random2_12.txt, 02_random2_13.txt, 02_random2_14.txt, 02_random2_15.txt, 02_random2_16.txt, 02_random2_17.txt, 02_random2_18.txt, 02_random2_19.txt, 02_random2_20.txt, 02_random2_21.txt, 02_random2_22.txt, 02_random2_23.txt, 02_random2_24.txt, 02_random2_25.txt, 02_random2_26.txt, 02_random2_27.txt, 02_random2_28.txt, 02_random2_29.txt, 02_random2_30.txt, 02_random2_31.txt, 02_random2_32.txt, 02_random2_33.txt, 02_random2_34.txt, 02_random2_35.txt, 02_random2_36.txt, 02_random2_37.txt, 02_random2_38.txt, 02_random2_39.txt, 02_random2_40.txt, 02_random2_41.txt, 02_random2_42.txt, 02_random2_43.txt, 02_random2_44.txt, 02_random2_45.txt, 02_random2_46.txt, 02_random2_47.txt, 02_random2_48.txt, 02_random2_49.txt, 02_random2_50.txt, 02_random2_51.txt, 02_random2_52.txt, 02_random2_53.txt, 02_random2_54.txt, 02_random2_55.txt, 02_random2_56.txt, 02_random2_57.txt, 02_random2_58.txt, 02_random2_59.txt, 02_random2_60.txt, 02_random2_61.txt, 02_random2_62.txt, 02_random2_63.txt, 02_random2_64.txt, 02_random2_65.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 04_handmade_00.txt, 04_handmade_01.txt, 04_handmade_02.txt, 04_handmade_03.txt, 04_handmade_04.txt, 04_handmade_05.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3504 KiB
00_sample_01.txt AC 1 ms 3488 KiB
00_sample_02.txt AC 1 ms 3572 KiB
00_sample_03.txt AC 1 ms 3572 KiB
01_random_00.txt AC 2 ms 3888 KiB
01_random_01.txt AC 2 ms 4088 KiB
01_random_02.txt AC 2 ms 3716 KiB
01_random_03.txt AC 1 ms 3620 KiB
01_random_04.txt AC 1 ms 3620 KiB
01_random_05.txt AC 1 ms 3732 KiB
01_random_06.txt AC 2 ms 3600 KiB
01_random_07.txt AC 2 ms 3564 KiB
02_random2_00.txt AC 2 ms 3540 KiB
02_random2_01.txt AC 2 ms 3708 KiB
02_random2_02.txt WA 2 ms 3612 KiB
02_random2_03.txt AC 2 ms 3600 KiB
02_random2_04.txt WA 2 ms 3704 KiB
02_random2_05.txt AC 2 ms 3544 KiB
02_random2_06.txt AC 2 ms 3612 KiB
02_random2_07.txt AC 2 ms 3676 KiB
02_random2_08.txt AC 2 ms 3776 KiB
02_random2_09.txt AC 2 ms 3700 KiB
02_random2_10.txt AC 2 ms 3680 KiB
02_random2_11.txt AC 2 ms 3708 KiB
02_random2_12.txt AC 2 ms 3652 KiB
02_random2_13.txt AC 2 ms 3588 KiB
02_random2_14.txt AC 2 ms 3680 KiB
02_random2_15.txt AC 2 ms 3700 KiB
02_random2_16.txt AC 2 ms 3544 KiB
02_random2_17.txt AC 2 ms 3688 KiB
02_random2_18.txt AC 2 ms 3704 KiB
02_random2_19.txt AC 2 ms 3704 KiB
02_random2_20.txt AC 2 ms 3624 KiB
02_random2_21.txt AC 2 ms 3664 KiB
02_random2_22.txt AC 2 ms 3704 KiB
02_random2_23.txt AC 2 ms 3692 KiB
02_random2_24.txt AC 2 ms 3660 KiB
02_random2_25.txt AC 2 ms 3648 KiB
02_random2_26.txt AC 2 ms 3688 KiB
02_random2_27.txt AC 2 ms 3592 KiB
02_random2_28.txt AC 2 ms 3616 KiB
02_random2_29.txt AC 2 ms 3664 KiB
02_random2_30.txt WA 2 ms 3684 KiB
02_random2_31.txt WA 2 ms 3616 KiB
02_random2_32.txt WA 2 ms 3692 KiB
02_random2_33.txt WA 2 ms 3764 KiB
02_random2_34.txt WA 2 ms 3684 KiB
02_random2_35.txt WA 2 ms 3588 KiB
02_random2_36.txt WA 2 ms 3704 KiB
02_random2_37.txt WA 2 ms 3704 KiB
02_random2_38.txt WA 2 ms 3724 KiB
02_random2_39.txt WA 2 ms 3656 KiB
02_random2_40.txt WA 2 ms 3700 KiB
02_random2_41.txt WA 2 ms 3588 KiB
02_random2_42.txt WA 2 ms 3780 KiB
02_random2_43.txt WA 2 ms 3584 KiB
02_random2_44.txt WA 2 ms 3648 KiB
02_random2_45.txt WA 2 ms 3668 KiB
02_random2_46.txt WA 2 ms 3648 KiB
02_random2_47.txt WA 2 ms 3664 KiB
02_random2_48.txt WA 2 ms 3656 KiB
02_random2_49.txt WA 2 ms 3664 KiB
02_random2_50.txt WA 2 ms 3632 KiB
02_random2_51.txt WA 2 ms 3700 KiB
02_random2_52.txt WA 2 ms 3776 KiB
02_random2_53.txt WA 2 ms 3672 KiB
02_random2_54.txt WA 2 ms 3676 KiB
02_random2_55.txt WA 2 ms 3684 KiB
02_random2_56.txt WA 2 ms 3664 KiB
02_random2_57.txt WA 2 ms 3688 KiB
02_random2_58.txt WA 2 ms 3644 KiB
02_random2_59.txt WA 2 ms 3684 KiB
02_random2_60.txt WA 2 ms 3664 KiB
02_random2_61.txt WA 2 ms 3648 KiB
02_random2_62.txt WA 2 ms 3668 KiB
02_random2_63.txt WA 2 ms 3700 KiB
02_random2_64.txt WA 2 ms 3644 KiB
02_random2_65.txt WA 2 ms 3640 KiB
03_random3_00.txt AC 2 ms 3588 KiB
03_random3_01.txt AC 2 ms 3652 KiB
03_random3_02.txt AC 2 ms 3688 KiB
04_handmade_00.txt AC 2 ms 3660 KiB
04_handmade_01.txt AC 1 ms 3408 KiB
04_handmade_02.txt AC 1 ms 3412 KiB
04_handmade_03.txt AC 2 ms 3700 KiB
04_handmade_04.txt WA 2 ms 3652 KiB
04_handmade_05.txt AC 2 ms 3776 KiB