hdu 6170 Two strings(dp)
来源:互联网 发布:sql数据库特点 编辑:程序博客网 时间:2024/05/20 10:53
Two strings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1420 Accepted Submission(s): 583
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
题意;
看两个字符串能否匹配,*表示前面一个字符可以匹配多次,'.'表示匹配可以任意字符,问两个字符能否匹配
解析:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define MAXN 3000char T[MAXN],S[MAXN];int dp[MAXN][MAXN];int n,m;int main(){int t;scanf("%d",&t);while(t--){scanf("%s",S+1);scanf("%s",T+1);n=strlen(S+1);m=strlen(T+1);memset(dp,0,sizeof(dp));dp[0][0]=1;for(int i=1;i<=m;i++){if(T[i]=='*') dp[i][0]=1; //例如ab,a*ab的情况,dp[2][0]就用于在匹配T[3]时dp[3][1]=dp[2][0],或ab,a*a*ab,dp[5][1]=dp[4][0],dp[3][1]=dp[2][0]for(int j=1;j<=n;j++) //若i,j可以匹配,那么dp[i][j]由dp[i-1][j-1]匹配的情况决定,若i,j之前的0都能匹配那么dp[i][j]=1{ //dp[i][j]表示在i,j之前的字符串都成功匹配if(T[i]=='.') dp[i][j]=dp[i-1][j-1]; else if(T[i]!='*') {if(T[i]==S[j]) dp[i][j]=dp[i-1][j-1];}else{dp[i][j]=max(dp[i-1][j],dp[i-2][j]); //dp[i-1][j]比较大表示*表示次数为1,dp[i-2][j]大表示*的次数为0if(dp[i][j-1]&&S[j]==S[j-1]) //T[i]=='*',*的次数>1的情况{if(T[i-1]=='.'||S[j]==T[i-1]) dp[i][j]=1; //如果S[j]==T[i-1]('*'前的字符即重复的字符)}}}}if(dp[m][n]) printf("yes\n");else printf("no\n");}return 0;}
阅读全文
0 0
- hdu 6170 Two strings dp
- hdu 6170 Two strings(DP)
- HDU 6170(Two strings-DP)
- hdu 6170 Two strings(dp)
- hdu 6170 Two strings dp模拟
- Hdu 6170 Two strings【思维+Dp】
- HDU 6170 Two strings (dp)
- HDU 6170 Two strings (二维DP)
- hdu 6170 Two strings(线性dp)
- HDU 6170 Two strings(dp)
- HDU 6170 Two strings dp || 正则
- hdu 6170 Two strings (dp)
- HDU 6170 Two strings (DP)
- HDU 6170 Two strings(DP)
- hdu 6170 Two strings
- HDU 6170:Two strings
- HDU 6170 Two strings
- hdu 6170 Two strings
- HTTP协议(请求行、请求头、请求体)详解
- 使用spring事件驱动机制,实现权限信息的自动收集存库
- 为取出的每个数组中增加一个自增的id(注意此ID不是数据库中存的自增id字段)
- oracle_常用函数汇总
- 关于监控安装原理图
- hdu 6170 Two strings(dp)
- ExpandableListView实现手风琴效果
- java spring中配置quartz定时任务
- 代码实现组合动画
- LeetCode With JS||26.Remove Duplicates from Sorted Array[数组去重,返回去重后的长度]
- nginx server_name 匹配顺序规则,新手必看
- io wait
- SElinux android 在hal下 如何获得对一个内核节点的访问权限
- Android内存优化