zoukankan      html  css  js  c++  java
  • Codeforces Round #464 (Div. 2) D题【最小生成树】

    Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.

    This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.

    More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.

    Input

    The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.

    The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.

    The third line contains the lettering on Tolya's t-shirt in the same format.

    Output

    In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.

    In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.

    If there are many optimal answers, output any.

    Examples

    Input
    3
    abb
    dad
    Output
    2
    a d
    b a
    Input
    8
    drpepper
    cocacola
    Output
    7
    l e
    e d
    d c
    c p
    p o
    o r
    r a

    Note

    In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'.

    Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.

    题意:用最少的字母对,使得两个字符串相等。字母对之间的字母可以相互转换。

    思路:每对字符之间建条边,然后跑并查集即可。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define int long long
     4 #define N 305500
     5 int arr[N];
     6 int f[N];
     7 int n;
     8 string s1,s2;
     9 struct str{
    10     int x,y;
    11 }st[N];
    12 vector<pair<int,int> > ans;
    13 int getf(int v){
    14     if(v==f[v]){
    15         return f[v];
    16     }else{
    17         f[v]=getf(f[v]);
    18         return f[v];
    19     }
    20 }
    21 int merge(int  u,int v){
    22     int t1=getf(u);
    23     int t2=getf(v);
    24     if(t1!=t2){
    25         f[t2]=t1;
    26         ans.push_back(make_pair(u,v));
    27         return 1;
    28     }
    29     return 0;
    30 }
    31 void init(){
    32     for(int i=0;i<=26+1;i++)
    33         f[i]=i;
    34 }
    35 signed main(){
    36     init();
    37     cin>>n>>s1>>s2;
    38     int cnt=0;
    39     for(int i=0;i<n;i++){
    40         if(s1[i]==s2[i])
    41             continue;
    42         else{
    43             st[cnt].x=s1[i]-'a';
    44                st[cnt].y=s2[i]-'a';
    45                cnt++;
    46             st[cnt].x=s2[i]-'a';
    47             st[cnt].y=s1[i]-'a';
    48             cnt++;    
    49         }
    50     }
    51     int add=0;
    52     for(int i=0;i<cnt;i++){
    53         if(merge(st[i].x,st[i].y)){
    54             add++;
    55         }
    56     }
    57     cout<<add<<'
    ';
    58     for(int i=0;i<ans.size();i++){
    59         cout<<(char)(ans[i].first+'a')<<" "<<(char)(ans[i].second+'a')<<'
    ';
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    热爱工作 发财机会大增(这里不是选择软件还是硬件的问题,是自己的性格和追求的问题)——当你的老板不如你懂行的时候,还赚的盆满钵满的时候,你就可以考虑独立了 good
    TaintDroid:智能手机监控实时隐私信息流跟踪系统(三)
    Delphi读取文件属性
    创业者该如何看待腾讯的“跟进”?
    IBM总裁郭士纳总结的四类人
    迅雷程浩:企业外包服务,下一个大的风口?(2B业务一定要懂销售和营销的人,这点和2C 不一样)
    dddd
    android:minSdkVersion 之我见
    dddd
    请人不怕啦
  • 原文地址:https://www.cnblogs.com/pengge666/p/11845370.html
Copyright © 2011-2022 走看看