HDU-6170 Two strings

本文介绍了一种基于正则表达式的字符串匹配算法,并提供了一个使用C++实现的具体示例。该算法能够处理包含特殊通配符'*'的正则表达式,用于匹配任意长度的字符序列。

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

http://acm.hdu.edu.cn/showproblem.php?pid=6170

. 匹配任意字符,x* 匹配任意长度的 x (x 为任意字符,长度可以为 0 )

正则表达式

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <regex>
#include <sys/types.h>

const int MAX_N = 3e3 + 7;


char s1[MAX_N], s2[MAX_N],sr[MAX_N * 1000];
inline bool solve() {
  gets(s1); gets(s2);
  std::regex rx("\\.\\*");
  char fmt[] = "(.)\\1*";
  *std::regex_replace(sr, s2, s2 + strlen(s2), rx, fmt) = '\0';
  return regex_match(s1,std::regex(sr));
  return false;
}

int main() {
  int T; scanf("%d", &T); getchar();
  while(T--) puts(solve() ? "yes" : "no");
  return 0;
}
  • 需要 sys/types.h , regex
  • regex_replace(s,first,last,rgx,tmt,flags) 目标地址,源首,源末,替换目标,替换内容,
  • regex_match(s,rgx) 源,匹配内容

转载于:https://www.cnblogs.com/Forgenvueory/p/7418315.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值