HDU 6170 Two strings dp || 正则
2017-08-23 15:57
387 查看
HDU 6170 Two strings dp || 正则
题目链接:two stringsTwo strings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1060 Accepted Submission(s): 435
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 “”.
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).
Output
For each test case, print “yes” if the two strings are matched, otherwise print “no”.
Sample Input
3
aa
a*
abb
a.*
abb
aab
Sample Output
yes
yes
no
Source
2017 Multi-University Training Contest - Team 9
题意:
2个串s和t,s包含大小写字母,t包含大小写和. 和* 。其中.代表通配,*代表可以出现任意次前一个字符,包括-1次把前面的抵消掉。问2个串是否匹配题目分析:
dp[i][j]代表t的前i个字符和s的前j个是否匹配,对于
s[j]==t[i]||t[i]=='.'状态转移为
dp[i][j]=dp[i-1][j-1];对于
t[i]=='*',考虑次数取0和-1的情况,只要
dp[i-1][j]或者dp[i-2][j]匹配,
dp[i][j]就会匹配。同时,考虑已经匹配过的情况,即
dp[i-1][j-1]||dp[i][j-1]并且
s[j]==s[j-1]时,
dp[i][j]=1
网上居然还有正则库通过的大牛,mark一下
// // main.cpp // HDU 6170 Two strings // // Created by teddywang on 2017/8/22. // Copyright © 2017年 teddywang. All rights reserved. // #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; char s[2551],t[2551]; bool dp[2600][2600]; int main() { int T; scanf("%d",&T); getchar(); while(T--) { s[0]=t[0]=1; gets(s+1); gets(t+1); int ls=strlen(s)-1; int lt=strlen(t)-1; memset(dp,0,sizeof(dp)); dp[0][0]=1; for(int i=1;i<=lt;i++) { if(i==2&&t[i]=='*') dp[i][0]=1; for(int j=1;j<=ls;j++) { if(t[i]=='.'||t[i]==s[j]) dp[i][j]=dp[i-1][j-1]; else if(t[i]=='*') { dp[i][j]=dp[i-1][j]|dp[i-2][j]; if((dp[i-1][j-1]||dp[i][j-1])&&s[j-1]==s[j]) dp[i][j]=1; } } } if(dp[lt][ls]==1) printf("yes\n"); else printf("no\n"); } }
使用
regex正则库:
// // main.cpp // 1010Two strings // // Created by teddywang on 2017/8/22. // Copyright © 2017年 teddywang. All rights reserved. // #include <iostream> #include<cstdio> #include<cstring> #include <regex> #include<string> #include<algorithm> #include<vector> using namespace std; int T; vector<string> tmp; int main() { scanf("%d",&T); while(T--) { string s,t; cin>>s>>t; int len=t.length(); tmp.clear(); for(int i=0;i<len;i++) { string tt=t.substr(i,1); if(t[i]=='*'&&t[i-1]=='.') continue; if(t[i]=='.'&&t[i+1]=='*') tt="(.)\\1*"; tmp.push_back(tt); } string buf; for(int i=0;i<tmp.size();i++) { buf+=tmp[i]; } regex re(buf); bool flag=regex_match(s,re); if(flag) printf("yes\n"); else printf("no\n"); } }
相关文章推荐
- hdu 6170 Two strings dp
- hdu 6170 Two strings dp
- Hdu 6170 Two strings【思维+Dp】
- 2017多校第9场 HDU 6170 Two strings DP
- 2017多校第9场 HDU 6170 Two strings DP
- HDU 6170 && 2017 多校训练:Two strings(DP)
- HDU - 6170 Two strings dp
- HDU 6170(Two strings-DP)
- 2017多校联合第9场1010 Two String/hdu 6170(正则表达式/dp)
- HDU 6170----Two strings(DP)
- Two strings HDU - 6170 递推
- hdu 6170 正则表达式 dp
- hdu 6170 正则表达式 dp
- 2017多校9 Two string hdu 6170 dp
- hdu 6170 正则表达式 dp
- hdu 6170 正则表达式 dp
- hdu 6170 正则表达式 dp
- hdu 6170 正则表达式 dp
- hdu 6170 正则表达式 dp
- hdu 6170 正则表达式 dp