uva1399 - Puzzle AC自动机+记忆化搜索

LiblibAI新手教程:从零开始训练你的第一个LoRA模型(保姆级指南) 本文提供LiblibAI平台上从零开始训练LoRA模型的保姆级教程,涵盖数据准备、参数优化到效果调优全流程。详细解析LoRA技术优势,包括云端计算资源、自动化预处理工具等,帮助新手快速掌握自定义模型训练技巧,实现高效AI绘画创作。 阅读详情

Jisung is the student representative of the Department of Computer Engineering in ACM University. A few days later, the annual festival will be held for the students in the department. He is preparing some events for the festival. Since Jisung likes to make and solve puzzles, he decides to devise some interesting puzzle for the festival.

The followings are the rules for the puzzle made by Jisung:

(1)
The players will be given an integer n. Then they should use the firstn capital letters from the Roman alphabet. For example, ifn = 4, the four characters A, B,C, and D will be used to solve this puzzle.
(2)
The players will be given s forbidden strings. No forbidden string contains another forbidden string as a substring. The winner is the student who makes the longest string that does not include a forbidden string as a substring.
(3)
If such a longest string does not exist, i.e., if we can make arbitrarily long strings that satisfy the above condition, or if we cannot make any string that satisfies the above condition, `No' should be answered.

For example, suppose the given number n = 2, i.e., the players can use the two charactersA and B. Assume that the forbidden strings are {AAA, AB, BA, BB}. In this case, the longest string that does not include any of the four forbidden strings as substrings isAA. But if the given forbidden strings are {AAA, BBB, ABAB, BBAA}, we cannot make such a longest string since arbitrarily long concatenations ofABA, i.e., ABAABAABA ... do not include any forbidden string.

Input 

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing two integersn (1$ \le$n$ \le$26) and s (1$ \le$s$ \le$1, 000) which represent the number of characters and the number of forbidden strings, respectively. From the second line to(s + 1)-st line of the test case, the forbidden strings are given one by one. The length of a forbidden string does not exceed 50.

Output 

Your program is to write to standard output. Print exactly one line for each test case. Print the longest string that does not include any forbidden string as a substring if it exists, otherwise, just print `No' as output. When there exists more than one such longest string with the same length, print the lexicographically largest string among them.

Sample Input 

3 
2 4 
AAA 
AB 
BA 
BB 
2 4 
AAA 
BBB 
ABAB 
BBAA 
3 7 
AA 
ABA 
BAC 
BB 
BC 
CA 
CC

Sample Output 

AA 
No 
ACBAB

  找一个串包含A到A+N,这个串不包括给的一些子串,使这个串最长。如果不存在或无限长输出No。

  先用子串建立AC自动机,只要是包含那些子串的串都标记,val[u]|=val[f[u]]。这样问题变成了搜索一个最长的串不经过标记节点,并且不出现环。

  dp[u]表示在AC自动机图上从u这个点开始(不包括u)能找到的最长串,如果一个点在之前已经被访问过,也就是说dp[u]!=-1,就直接返回。那会不会u开始的最长串和当前搜索到u的这个串出现环呢?是不会的,因为u的最长串如果经过搜索到u之前的某个点,那个点的dp[u]!=-1,这个串搜到那个点的时候就已经返回了,不会搜到u了。

  用vis表示当前路径上的点,只要在搜索过程中发现环,答案就是No了。

  更新dp的时候记录路径,如果不是No的话打印路径。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXN 60010
#define MAXM 2000010
#define MAXNODE 50010
#define MOD 999983
#define SIGMA_SIZE 26
typedef long long LL;
using namespace std;
int T,N,M,vis[MAXNODE],dp[MAXNODE],next[MAXNODE];
char str[100];
struct AC{
    int ch[MAXNODE][SIGMA_SIZE],f[MAXNODE],val[MAXNODE],sz;
    void init(){
        memset(ch[0],0,sizeof(ch[0]));
        val[0]=0;
        sz=1;
    }
    int idx(char c){
        return c-'A';
    }
    void insert(char *s,int v){
        int u=0;
        for(int i=0;s[i];i++){
            int c=idx(s[i]);
            if(!ch[u][c]){
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]=1;
    }
    void get_fail(){
        queue<int> q;
        f[0]=0;
        for(int c=0;c<SIGMA_SIZE;c++){
            int u=ch[0][c];
            if(u){
                f[u]=0;
                q.push(u);
            }
        }
        while(!q.empty()){
            int r=q.front();
            q.pop();
            for(int c=0;c<N;c++){
                int u=ch[r][c];
                if(!u){
                    ch[r][c]=ch[f[r]][c];
                    continue;
                }
                q.push(u);
                int v=f[r];
                while(v&&!ch[v][c]) v=f[v];
                f[u]=ch[v][c];
                val[u]|=val[f[u]];
            }
        }
    }
}ac;
int DFS(int u){
    if(vis[u]) return -1;
    int &ans=dp[u];
    if(ans!=-1) return ans;
    vis[u]=1;
    ans=0;
    for(int c=N-1;c>=0;c--){
        int v=ac.ch[u][c];
        if(!ac.val[v]){
            int tmp=DFS(v);
            if(tmp==-1) return -1;
            if(tmp+1>ans){
                ans=tmp+1;
                next[u]=c;
            }
        }
    }
    vis[u]=0;
    return ans;
}
void print(int u){
    if(next[u]!=-1){
        printf("%c",next[u]+'A');
        print(ac.ch[u][next[u]]);
    }
}
int main(){
    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&N,&M);
        ac.init();
        while(M--){
            scanf("%s",str);
            ac.insert(str,1);
        }
        ac.get_fail();
        memset(next,-1,sizeof(next));
        memset(dp,-1,sizeof(dp));
        memset(vis,0,sizeof(vis));
        if(DFS(0)!=-1&&dp[0]>0){
            print(0);
            puts("");
        }
        else printf("No\n");
    }
    return 0;
}



机械臂控制新思路:用自适应强化学习搞定动态避障(含Matlab/Simulink仿真文件) 本文探讨了自适应强化学习在机械臂动态避障控制中的应用新思路。通过对比传统PID与MPC方法的局限,详细阐述了自适应强化学习如何让机械臂在未知动态环境中自主学习避障策略,并提供了基于Matlab/Simulink的仿真实现与工程化部署指南。 阅读详情

相关推荐

黑群晖二合一已损毁_手动修复黑群晖已损毁磁盘空间

lz的用法是这样的,一个中古级mac mini 2011上装有vmware esxi 7.6, 然后上面挂载了lz的华芸Asustor Nas的iscsi空间,lz用的是basic volume,然后esxi上面有虚拟的黑群晖6.2.2-24922,黑群晖的docker里跑着不可描述软件,但是发现下载速度过快时,比如有好几个文件同时下载,总速度超过60MB/s以后storage manager里很...

weixin_39669629的博客 1万+

AC自动机+记忆化搜索uva1399Puzzle

Jisung is the student representative of the Department of Computer Engineering in ACM University. A few days later, the annual festival will be held for the students in the department. He is preparing

龙崎的专栏 592

CCSv3.3安装避坑全记录:从补丁失败到XDS510驱动,手把手搞定DSP开发环境

本文详细记录了CCSv3.3安装过程中的常见问题及解决方案,包括补丁安装失败、XDS510驱动识别异常等。通过系统环境准备、安装路径优化、补丁顺序调整等步骤,手把手帮助开发者搭建稳定的DSP开发环境,特别适合高校DSP实验课使用。

weixin_33494995的博客 328

uva 1399 - Puzzle(AC自动机)

题目链接:uva 1399 - Puzzle 题目大意:给定K和N,表示有K种不同的字符,N个禁止串,求一个最长的串使得该串不包含任何禁止串为子串。如果存在循环或者不能构成的话,输出No。 解题思路:建立AC自动机,然后在AC自动机上做dp,所有单词结尾节点为禁止点。 #include #include #include #include using namespace s

不慌不忙、不急不躁 1353

【Gephi】初学者教程(一)「一步一步教你怎么画图」「值得放进收藏夹吃灰系列」

前言 由于最近博主日常接任务帮人画图,而最近的状况大家也知道…远程画着画着觉得双方在图的结构、调色等等诸多方面会有很多歧义,博主理解的往往与客户所言有些许偏差…当然这其中也是包括有客户并未接触过gephi而导致的一些原因吧emmmm 总而言之,今天突发奇想在这个时候出教程就是为了让大噶少走点弯路(虽然博主才是真的菜) 然后呢,想说的比较多,所以就多拆了几个章节咳咳… 朋友们如果急切需要什么的可以直...

修仙一时爽 早起头犯怅 4815

TLF35584 窗口看门狗_理论篇

1)看门狗的结果(有效或无效触发)由相关的看门狗故障计数器独立监控。2)窗口看门狗的状态是WWO,它可能有“有效WWD触发”或“无效WWD触发”值。

舊梦的博客 2532

UVA 1399 - Puzzle(AC自动机+DP)

UVA 1399 - Puzzle 题目链接 题意:给定一些字符串,求一个最长的不在包含这些子串的字符串,如果可以无限长输出No 思路:建ACM自动机,把不可走结点标记构造出来,然后在这个状态图上进行dp找出最长路径即可,至于无限长的情况,只要在dp前进行一次dfs判有没有环即可 代码: #include #include #include #include

Remilia's 1269

Revit获取族预览图

最近做到和族库相关的功能需要获取到族文件的预览图,在RevitAPI中提供了ElementType.GetPreviewImage方法,但是前提就是你是在Revit里面操作的,并且已经拿到了Symbol对象实例,才可以调用此方法。然后在网上找了另外一种方式,通过读取Revit文件,在REVITPREVIEW4.0字段截取文件信息,将其转换为图片格式。 一、使用RevitAPI中ElementType.GetPreviewImage方法 [Autodesk.Revit.Attributes.Transac.

weixin_44631419的博客 1759

LA 3907 Puzzle(AC自动机)

题意:给定n个禁止串,问不包含禁止串作为子串的字符串的最长长度,如何长度可以无穷大或者为0输出No 思路:在AC自动机上进行DP,dp[u]代表当前这个结点为起点(代表已经匹配了的字符串的后缀),最多能够匹配多少个字符,然后resstr[u]代表最多能够匹配的字典序最大的字符串,然后就直接dfs一遍,每次转移的时候要注意不能够进入到禁止节点,同时我们也知道有可能一个节点对应字符串的后缀是禁止字符串,

latstars的博客 341

【ComfyUI专栏】安装Triton组件

Triton是一款支持大模型推理的深度学习加速组件,最初仅支持Linux平台,现已通过开源社区适配Windows系统。针对SageAttention的安装,建议使用3.2.0Post10版本(该版本提供封装好的安装包,更高版本仅有源码)。用户可通过GitHub获取安装资源,或直接使用pip命令"pip install triton-windows"进行安装。安装时需注意:1)在虚拟环境中操作;2)使用管理员权限。相关资源链接已在文中提供。

技术与管理的平衡木- 1881

【RK3399 Ubuntu】基于rk3399的lxde的lubuntu桌面移植适配记录之第二步:最基本Ubuntu根文件系统的相关常用工具完善

文章目录1.使能系统网络管理服务1.1 安装驱动modules管理工具1.2 使能网络服务2.终端自动补全命令功能3.安装ssh服务4.安装系统信息查看工具5.安装resize工具6.安装lrzsz工具7.使能rc.local服务7.1 修改服务Install块7.2 使能rc.local.service服务7.3 添加执行脚本且赋予执行权限 续接上文,本文主要完善前面已经制作好的rootfs 1.使能系统网络管理服务 1.1 安装驱动modules管理工具 apt-get install kmod 1.

wang 恒 的博客 1394

记忆化搜索---最长子串

问题:两个字符串求其最长可不连续相同子串,打印出子串 思考:这样的问题可以用回溯的方法,通过递归把每种情况都遍历一遍,再得出最佳结果,但是这样的解法的时间发杂度为2的n次方,当字符串长度达到10以上时首先空间会溢出,再者时间消耗太多。所以考虑用记忆化搜索,用二维数组存储两个字符串中没两个元素之间的关系。 在用二维数组存储关系的难题在于如何去遍历,让每个节点的值是到达它的最大值,从子串的定义可以...

张元胤的博客 393

Unity零基础到入门 ☀️| 【UI系统学习】轻松学会 Unity 的 UGUI基础控件的使用(包括一个简易血条、蓝条设计)

Unity,UGUI。本篇博客来挑几种Unity中最常用的UI控件如何使用和使用技巧,包括一个简易血条蓝条的小实例

努力前行,总会成为自己心中的那道光 2万+

【题解】UVA1399 Puzzle AC自动机上DP,有向图最长路径

给定可用字符数n(26)和s(1000)个长度不超过50的模式串,输出一个不包含任一模式串的字典序最大的最长文本串。长度为0或无限时输出"No". 对所有模式串建AC自动机,标记终结点和它们在fail树上的子节点为不可用节点。问题就转换成了在Trie图的所有可用节点上求一个从0节点开始的最长路径。 从0开始记忆化搜索来查找最长路径,维护三个数组vis、dp、nxt: vis记录访问状态,1表示正...

秋叶红于二月花 635

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

UVa1399/LA3907 Puzzle

本人学习icpc算法竞赛时自己对UVa部分题目的解题思路 给定m和n,表示有m种不同的字符(大写字母A,B,C⋯),n 个禁止串(n≤1000,每个串长度不超过50),请构造一个不包含任何禁止串的最长字符串并将其输出。如果可以无限长或者无解则输出No,如果存在多解则输出字典序最大那个。AC自动机加记忆化dfs。

hlhgzx的博客 461

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

hdu5384 Danganronpa AC自动机 多校联合第八场

Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 13    Accepted Submission(s): 1 Problem Description Danganronpa is a vide

837

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

ZOJ Problem Set - 3494 BCD Code AC自动机+数位DP

BCD Code Time Limit: 5 Seconds      Memory Limit: 65536 KB Binary-coded decimal (BCD) is an encoding for decimal numbers in which each digit is represented by its own binary sequence.To enc

821

hdu2243 考研路茫茫——单词情结 AC自动机+矩阵快速幂

考研路茫茫——单词情结 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3952    Accepted Submission(s): 1150 Problem Description 背单词,始终是复习英语的重要环节。在

680

ZOJ Problem Set - 3228 Searching the String AC自动机

Searching the String Time Limit: 7 Seconds      Memory Limit: 129872 KB Little jay really hates to deal with string. But moondy likes it very much, and she's so mischievous that she often g

637

uva1076 - Password Suspects AC自动机+状态压缩DP

You are the computer whiz for the secret organization known as the Sneaky Underground Smug Perpetrators of Evil Crimes and Thefts. The target for SUSPECT's latest evil crime is their greatest foe, t

589

uva11019 - Matrix Matcher 二维矩阵匹配 AC自动机

Problem H Matrix Matcher Input: Standard Input Output: StandardOutput   Given an N * M matrix, your task is to find the number of occurences of an X * Y pattern.   Input The first line contai

579

hdu3247 Resource Archiver AC自动机+状态压缩DP

Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 1928    Accepted Submission(s): 625 Problem Description Great! Yo

571

hdu 2296 Ring AC自动机+DP

Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2427    Accepted Submission(s): 753 Problem Description For the hope of a forever

521

AC自动机

AC自动机是tire树和KMP的结合,若模版串长度为l,有n个模版串,文本串长度为s,则AC自动机复杂度为O(l*n+s)。   KMP是一个模版串和一个文本串,最常见的是问模版串在文本串中的出现次数。所以失配的时候就去找模版串和文本串当前后缀匹配最长的前缀,若再失配以此类推。   但是若有多个模版串,若和当前模版串失配,可能会和其他模版串匹配,或者其他模版串的前缀能比当前模版串的前缀和文本串

518

poj2778 DNA Sequence AC自动机+矩阵快速幂

DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11721   Accepted: 4471 Description It's well known that DNA Sequence is a sequence only contains

517

poj1625 Censored! AC自动机+DP

Censored! Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 8108   Accepted: 2193 Description The alphabet of Freeland consists of exactly N letters. Each sent

515
上一篇: hdu 4973 A simple simulation problem 线段树
下一篇: poj2778 DNA Sequence AC自动机+矩阵快速幂
小小小小葱
博客等级 码龄14年 22粉丝 266原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值