HDU-6170 Two strings

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) = '';
  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