Codeforces 623A Graph and String (构造)

本文介绍了一道 CodeForces 上的字符串构造题,题目要求根据给定的字符连接关系构造一个仅包含abc三个字母的字符串。文章详细解释了解题思路,并提供了一个 C++ 实现的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/problemset/problem/623/A


题意:一个仅包含abc这3个字母的字符串,a与b和自身相连,b与ac和自身相连,c与b与自身相连,给出字符串每一位的相连情况,要求构造出这样一个字符串


思路:b与ac和自己都相连,所以连有n-1个点的位置必定是b,其他的点我们找一点和与该点相连的点设为a,其他设为c,最后n^2判断是否符合题意就可以



#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int d[530],vis[530],Link[530][530];
vector <int> G[530];
char ans[530];

void dfs(int u)
{
    if (ans[u]=='b')
        return;
        ans[u]='a';
    for (int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if (!vis[v])
        {
            vis[v]=1;
            dfs(G[u][i]);
        }
    }
}


int main()
{
    int n,m;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        memset(G,0,sizeof(G));
        memset(d,0,sizeof(d));
        memset(ans,0,sizeof(ans));
        memset(vis,0,sizeof(vis));
        memset(Link,0,sizeof(Link));
        for (int i=0;i<m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            Link[a][b]=1;
            Link[b][a]=1;
            G[a].push_back(b);
            G[b].push_back(a);
            d[a]++;
            d[b]++;
        }

        for (int i=1;i<=n;i++)
        {
            if (d[i]==n-1)
                ans[i]='b';
        }

        for (int i=1;i<=n;i++)
        {
            if (ans[i]!='b')
            {
                dfs(i);
                break;
            }
        }

        for (int i=1;i<=n;i++)
        {
            if (ans[i]!='a' && ans[i]!='b')
            {
                ans[i]='c';
            }
        }

        int flag=1;
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=n;j++)
            {
                if (i==j) continue;
                if (ans[i]==ans[j])
                {
                    if (!Link[i][j])
                        flag=0;
                }
            }
        }

        if (flag) printf("Yes\n%s\n",ans+1);
        else printf("No\n");

    }
}








You are given a positive integer N and two strings S and T, each of length N and consisting of lowercase English letters. Determine whether it is possible to make S identical to T by repeating the operation below any number of times (possibly zero). If it is possible, also find the minimum number of operations required. Choose two lowercase English letters x,y and replace every occurrence of x in S with y. Constraints 1≤N≤2×10 5 N is an integer. Each of S and T is a string of length N, consisting of lowercase English letters. Input The input is given from Standard Input in the following format: N S T Output If it is possible to make S identical to T, print the minimum number of operations required. Otherwise, print −1. Sample Input 1 Copy 6 afbfda bkckbb Sample Output 1 Copy 4 By performing the operation four times in the following way, you can make S identical to T: Choose x= b and y= c. S becomes afcfda. Choose x= a and y= b. S becomes bfcfdb. Choose x= f and y= k. S becomes bkckdb. Choose x= d and y= b. S becomes bkckbb, which is identical to T. It cannot be done with fewer than four operations, so the minimum number of operations required is 4. Sample Input 2 Copy 4 abac abac Sample Output 2 Copy 0 S and T are already identical, so no operations are required. Sample Input 3 Copy 4 abac abrc Sample Output 3 Copy -1 No matter how you repeat the operation, it is impossible to make S identical to T. Sample Input 4 Copy 4 abac bcba Sample Output 4 Copy 4 C++,中文!!!
最新发布
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值