Codeforces 514C Watto and Mechanism (字典树+dfs)

本文介绍了解决 CodeForces 514C 问题的方法,该问题要求判断一系列字符串是否能通过一次变换匹配给定的模式串。文章通过构建字典树,并使用 DFS 深度优先搜索来高效地查找每个输入字符串是否与字典树中的模式串相差一个字符。

题目链接:http://codeforces.com/problemset/problem/514/C


题意:给出n个模式串,给出m个字符串,问字符串能否通过变换一次成为模式串(必须变换)


思路:利用模式串建立字典树,dfs查询是否有何m个字符串相差一位的模式串(每一个字符都有可能变换,开始写的时候以为只有当前字符不符合才进行变换,无法处理一些情况)


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 600300
using namespace std;

struct Node
{
    int ed;
    Node *Next[5];
};

Node *tree=new Node;

char str[maxn];


void Insert(char s[])
{
    int l=strlen(s);
    Node *p=tree;
    for (int i=0; i<l; i++)
    {
        int t=s[i]-'a';
        if (p->Next[t]==NULL)
        {
            Node *newnode=new Node();
            for (int j=0; j<5; j++)
                newnode->Next[j]=NULL;
            newnode->ed=0;
            p->Next[t]=newnode;

        }
        p=p->Next[t];
    }
    p->ed=1;
}

int dfs(Node *p,int now,int flag)
{
    int t=str[now]-'a';
    if (str[now])
    {cout<<now<<":"<<flag<<endl;
        if (p->Next[t]!=NULL)
        {
            if (dfs(p->Next[t],now+1,flag))
                return 1;
        }
        if (flag)
        {
           // flag=0;
            for (int i=0; i<3; i++)
            {
                if (p->Next[i]!=NULL && i!=t)
                {
                    if (dfs(p->Next[i],now+1,0))
                        return 1;
                }
            }
        }

    }
    else
    {
        if (p->ed==1 && flag==0) return 1;

    }
return 0;
}

void init()
{
    for (int i=0;i<5;i++){//cout<<":"<<tree->Next[i];
     tree->Next[i]=NULL;
     }
    tree->ed=0;
}


int main()
{
    int n,m;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        for (int i=0;i<n;i++)
        {
            scanf("%s",&str);
            Insert(str);
        }

        for (int i=0;i<m;i++)
        {
            scanf("%s",&str);
            int l=strlen(str);
            if (dfs(tree,0,1)) printf("YES\n");
            else  printf("NO\n");
        }
    }
}


当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值