HDU6170-Two strings
来源:互联网 发布:乒乓球入门知乎 编辑:程序博客网 时间:2024/05/22 01:48
Two strings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 492 Accepted Submission(s): 183
Problem Description
Giving two strings and you should judge if they are matched.
The first string contains lowercase letters and uppercase letters.
The second string contains lowercase letters, uppercase letters, and special symbols: “.” and “*”.
. can match any letter, and * means the front character can appear any times. For example, “a.b” can match “acb” or “abb”, “a*” can match “a”, “aa” and even empty string. ( “*” will not appear in the front of the string, and there will not be two consecutive “*”.
The first string contains lowercase letters and uppercase letters.
The second string contains lowercase letters, uppercase letters, and special symbols: “.” and “*”.
. can match any letter, and * means the front character can appear any times. For example, “a.b” can match “acb” or “abb”, “a*” can match “a”, “aa” and even empty string. ( “*” will not appear in the front of the string, and there will not be two consecutive “*”.
Input
The first line contains an integer T implying the number of test cases. (T≤15)
For each test case, there are two lines implying the two strings (The length of the two strings is less than 2500).
For each test case, there are two lines implying the two strings (The length of the two strings is less than 2500).
Output
For each test case, print “yes” if the two strings are matched, otherwise print “no”.
Sample Input
3aaa*abba.*abbaab
Sample Output
yesyesno
Source
2017 Multi-University Training Contest - Team 9
题意:有两个字符串,问你第二个字符串和第一个字符串能否匹配,第二个字符串有两种符号,’.’可以匹配任意字符,’*’表示前一个字符可以重复零次或多次
解题思路:比赛时发现了可以n^2跑,可是居然没想到dp,一直想着暴力匹配,太菜了,dp还是很好推的,dp[i][j]表示第二个字符串前i个字符和第一个字符串前j个字符能否匹配
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <map> #include <set> #include <stack> #include <queue> #include <vector> #include <bitset> #include <functional> using namespace std;#define LL long long const int INF = 0x3f3f3f3f;int dp[2505][2505];char s1[2505], s2[2505];int main(){int t;scanf("%d", &t);while (t--){scanf("%s%s", s1 + 1, s2 + 1);memset(dp, 0, sizeof dp);int len1 = strlen(s1 + 1), len2 = strlen(s2 + 1);dp[0][0] = 1;for (int i = 1; i <= len2; i++){if (s2[i] == '*' && i == 2) dp[i][0] = 1;for (int j = 1; j <= len1; j++){if (s2[i] == '.') dp[i][j] = dp[i - 1][j - 1];else if (s2[i] != '*'){if (s2[i] == s1[j])dp[i][j] = dp[i - 1][j - 1];}else{dp[i][j] = max(dp[i - 2][j], dp[i - 1][j]);if (dp[i][j - 1] && s1[j] == s1[j - 1]){if (s2[i - 1] == s1[j] && s2[i - 1] != '.') dp[i][j] = 1;else if (s2[i - 1] == '.') dp[i][j] = 1;}}}}if (dp[len2][len1]) printf("yes\n");else printf("no\n");}return 0;}
阅读全文
0 0
- HDU6170-Two strings
- HDU6170-Two strings
- hdu6170 Two strings【regex真好用】
- [正则表达式] hdu6170 two strings
- Two strings hdu6170 dp递推
- HDU6170-Two strings 多校9 dp
- HDU6170 Two strings(动态规划)
- hdu6170-多看几遍之DP&递推&字符串-Two strings
- HDU6170 Two strings dp 多校联赛第9场
- HDU6170 Two strings(dp,2017 HDU多校联赛 第9场)
- hdu6170
- Two Strings
- BNU29375:Two Strings
- BNU 29375 Two Strings
- Two Strings Are Anagrams
- Two Strings Are Anagrams
- Two Strings Are Anagrams
- Two Strings Are Anagrams
- eclipse远程调用Hadoop时遇到的问题(1)
- Kaggle正式赛首战top3%分享,小白献给机器学习新手
- 林纳斯·本纳第克特·托瓦兹(Linus Benedict Torvalds)- Linux之父
- 小论文投稿
- PAT A 1105. Spiral Matrix (25)
- HDU6170-Two strings
- MAC下pyenv和pyenv-virtualenv插件初探
- 阿牛的EOF牛肉串
- HTML5推出的理由
- 《C++ Concurrency In Action》part1 HelloWorld
- 神经概率语言模型
- Git的奇技淫巧
- 意识形态和左右派之争
- JAVA访问权限控制(JAVA Access Control)