codeforces 128B. String Problem (floyd预处理)

题意:

给两个字符串,以及m个从字符a变换到b的代价,问把两个字符串变得相同的最小代价

思路:

将字符串变换的代价当做一条有向边建图,之后跑floyd,得到从字符i变成j的最小代价dis[i][j]

之后遍历两个字符串的每一位,不相同的话枚举变成什么字符所需要的代价最小,如果找不到这样一个中间点使得s1[i]=ch,s2[i]=ch,则直接输出-1,最后将最小代价累加就好了

#include<iostream>
#include<algorithm>
#include<cstring>
#define inf 0x3f3f3f3f
 using namespace std;
 const int maxn=1e5+10;
 int dis[27][27],mp[27][27];
 char s1[maxn],s2[maxn],a1,a2,s3[maxn];
 void floyd()
 {
     for(int i=0;i<26;i++)
         for(int j=0;j<26;j++)    dis[i][j]=mp[i][j];
     for(int k=0;k<26;k++)
         for(int i=0;i<26;i++)
             for(int j=0;j<26;j++)
                 dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
 }
 int main()
 {
     int n,w;
     cin>>s1>>s2>>n;
     for(int i=0;i<26;i++)
         for(int j=0;j<26;j++){
             if(i==j)    mp[i][j]=0;
             else mp[i][j]=inf;
         }
     for(int i=1;i<=n;i++){
         cin>>a1>>a2>>w;
         int u=a1-‘a‘,v=a2-‘a‘;
         mp[u][v]=min(mp[u][v],w);
     }
    floyd();
    int len1=strlen(s1),len2=strlen(s2);
    if(len1!=len2){
        cout<<-1<<endl;
        return 0;
    }
    int ans=0;
    for(int i=0;i<len1;i++){
        if(s1[i]==s2[i]){s3[i]=s1[i];continue;}
        else{
            int mini=inf,x=-1,u=s1[i]-‘a‘,v=s2[i]-‘a‘;
            for(int j=0;j<26;j++){
                if(dis[u][j]+dis[v][j]<mini) mini=dis[u][j]+dis[v][j],x=j;
            }
            if(x==-1){
                cout<<"-1"<<endl;
                return 0;
            }
            else{
                s3[i]=x+‘a‘;
                ans+=mini;
            }
        }
    }
    cout<<ans<<endl<<s3<<endl;
    return 0;
 }

原文地址:https://www.cnblogs.com/overrate-wsj/p/12271310.html

时间: 2024-10-28 14:41:16

codeforces 128B. String Problem (floyd预处理)的相关文章

Problem B Codeforces 295B 最短路(floyd)

Description Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game: The game consists

codeforces Round 286# problem A. Mr. Kitayuta&#39;s Gift

Mr. Kitayuta has kindly given you a string s consisting of lowercase English letters. You are asked to insert exactly one lowercase English letter into s to make it a palindrome. A palindrome is a string that reads the same forward and backward. For

codeforces 706C Hard problem DP(动态规划)问题

题目链接:http://codeforces.com/problemset/problem/706/C 题目大意:  给定n个字符串, 每个字符串可以颠倒前后位置(第一个字母到最后一个,第二个字母到倒数第二位) 每次颠倒需要花费ci的力气, 要求将所给的n个字符串用最小力气按字典序排列, 输出力气值, 如果无法按字典序排列, 则输出-1 数据范围:2?≤?n?≤?100?000 . ci (0?≤?ci?≤?1e9) 所有字符串总长度不会超过1000000. 解题思路: 这是一道DP题, dp[

HDU 5008 Boring String Problem(西安网络赛B题)

HDU 5008 Boring String Problem 题目链接 思路:构造后缀数组,利用height的数组能预处理出每个字典序开始的前缀和有多少个(其实就是为了去除重复串),然后每次二分查找相应位置,然后在往前往后找一下sa[i]最小的 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int

Codeforces 118A String Task

题目链接 http://codeforces.com/problemset/problem/118/A #include<iostream> #include<string> #include<cctype> #include<algorithm> using namespace std; int main() { string str; cin>>str; int i; //将代码变成小写 transform(str.begin(), str.

CodeForces 687A NP-Hard Problem

Portal:http://codeforces.com/problemset/problem/687/A 二分图染色 好模板题 有SPJ 值得注意的是,因为C++的奇妙的运算机制 若在vector变量x.size()=0,则x.size()-1会溢出(此处坑了我3h) 这不禁让我想起了文明2里的甘地 1 #include<iostream> 2 #include<algorithm> 3 #include<set> 4 #include<cstdio> 5

hdu 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que

HDOJ3374 String Problem [KMP最小循环节点]+[最小(大)表示法]

String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1442    Accepted Submission(s): 645 Problem Description Give you a string with length N, you can generate N strings by left shifts

String Problem hdu 3374 最小表示法加KMP的next数组

String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1492    Accepted Submission(s): 662 Problem Description Give you a string with length N, you can generate N strings by left shifts.