HDU 6170 字符串Dp
来源:互联网 发布:网络映射地址 编辑:程序博客网 时间:2024/05/09 17:35
Two strings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1222 Accepted Submission(s): 507
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
比如 a * 可以匹配 aaa 和 aaaaaaa等等
这个题贪心是贪不过的,用DP可以,而且字符串长 2500 ,n方的复杂度是允许的
dp[i][j]表示b[i]
对于 ' . ' ,它可以匹配任意字符,所以DP[i[[j] = DP[i - 1][j - 1]
对于' * ' ,它匹配一个的时候状态来自 DP[i - 1][j] 匹配 0 个的时候来自 DP[i - 2][j]
并且
if((dp[i - 1][j - 1] || dp[i][j - 1]) && a[j - 1] == a[j]){
dp[i][j] = 1;
}
#include<cstdio>#include<algorithm>#include<iostream>#include<string.h>using namespace std;#define maxn 2600int dp[maxn][maxn];char a[maxn],b[maxn];int n;int main(){scanf("%d",&n);getchar();while(n--){memset(dp,0,sizeof(dp));gets(a + 1);gets(b + 1);int lena = strlen(a + 1);int lenb = strlen(b + 1);dp[0][0] = 1;for(int i = 1;i <= lenb;i++){if(i == 2 && b[i] == '*')dp[i][0] = 1;for(int j = 1;j <= lena;j++){if(b[i] == '.' || a[j] == b[i]){dp[i][j] = dp[i - 1][j - 1];}else if(b[i] == '*'){dp[i][j] = dp[i - 1][j] | dp[i - 2][j];if((dp[i - 1][j - 1] || dp[i][j - 1]) && a[j - 1] == a[j]){dp[i][j] = 1;}}}}puts(dp[lenb][lena]?"yes":"no");}return 0;}
阅读全文
0 0
- HDU 6170 字符串Dp
- hdu 4433 字符串变化 DP
- hdu 4295 4 substrings problem DP 字符串
- Hdu 4545 魔法串 字符串dp
- hdu 4628 字符串状压dp
- HDU - 4271(经典字符串dp改)
- HDU 3689 字符串匹配 + 概率DP
- hdu 6170 Two strings dp
- hdu 6170 正则表达式 dp
- hdu 6170 Two strings(DP)
- HDU 6170(Two strings-DP)
- hdu 6170 Two strings(dp)
- hdu 1513 && poj 1159 线性dp回文字符串
- hdu 4632 子字符串统计的区间dp
- 字符串练习题: 【HDU 2457】 DNA repair(AC自动机+DP)
- hdu 1502 字符串dp与高精度运算(mark)
- HDU 4622 Reincarnation(字符串hash,hashMap,区间dp)
- 字符串DP——HDU 6017 Girls Love 233
- 关于C#编写x86与x64程序的分析
- 双倍回文(BZOJ-2342)(manacher+并查集)
- Kaldi WFST 构图 学习
- 国际化饥和屏幕适配 沉浸式
- 使用jsoup,爬取省/市/区/县/镇/乡 地域划分消息
- HDU 6170 字符串Dp
- LintCode_149_买卖股票的最佳时机
- XListView上拉加载下拉刷新
- springmvc参数绑定
- larave框架安装
- HTML5 canvas画圆
- 集训第二十四天(2017/8/23):二维树状数组&三维树状数组
- C/C++,用0x3f3f3f3f表示无穷大,0xc0c0c0c0表示无穷小
- (reading)Deep Visual-Semantic Alignments for Generating Image Descriptions