HDU6170 字符串DP/正则表达式

Two strings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2573    Accepted Submission(s): 912

 

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

 

多校重现,虐,嘤(一下午一道题可还行嘤

题意:第一段是只有字母的字符串,第二段是有字母以及特殊字符的字符串,'.'可以代替任意字符,'*'可以使它前一位的字符出现任意次数,甚至是0次(此时即*及其前面的字符匹配空字符串),问两串字符串是否匹配。

可能情况太多,在匹配前面的时候永远不知道后面有什么情况,用dp数组,dp[i][j]表示到字符串1第i位与字符串2第j位(前i与前j)的匹配情况:

1)s2[j]为'.'或者是与s1[i]匹配的字符,dp[i][j]=max(dp[i-1][j-1],dp[i][j]);

2)s2[j]为'*',情况比较多,比较复杂:

a.如果是在第二位,单独将dp[0][2]置1;

b.如果'*'之前两位的s2[j-2]与s1[i]及其之前都匹配,那么j位也匹配,此时即s2[j-1]出现0次,匹配空字符串。例如:ab与abb*。可以得出:dp[j][i]=max(dp[j][i],dp[j-2][i]);

c.如果'*'跟随的s2[j-1]与s1[i]及其之前都匹配,那么j位也匹配,此时即s2[j-1]出现1次。例如:ab与ab*。可以得出:dp[j][i]=max(dp[j][i],dp[j-1][i]);

d.如果s2[j-1]与s1[i-1]及其之前都匹配,且s1[i]=s1[i-1],此时即s2[j-1]出现2次。例如:abb与ab*。可以得出:dp[j][i]=max(dp[j][i],dp[j-1][i-1]||s1[i]==s1[i-1]);

e.如果s2[j]与s1[i-1]及其之前都匹配,且s2[j-1]与s1[i]匹配,此时即s2[j-1]出现3次及以上。例如:abbbbb与ab*。可以得出:dp[j][i]=max(dp[j][i],dp[j][i-1]&&(s2[j-1]==s1[i]||s2[j-1]=='.'&&s1[i-1]==s1[i]));

注:e情况不可单纯写s1[i]==s1[i-1],例aa与ab*,因为dp[j][i-1]=1且s1[i]=s1[i-1],就无法返回正确答案(虽然辣鸡HDU数据太弱一样ac)

 

code:

#include 
#include 
#include 
#include 

using namespace std;

const int maxs=2500;

int dp[maxs+10][maxs+10];

int main()
{
    int t;
    char s1[maxs+10],s2[maxs+10];
    int i,j,l1,l2;

    scanf("%d",&t);
    while(t--){
		memset(dp,0,sizeof(dp));
		scanf("%s",s1+1);
		scanf("%s",s2+1);
		l1=strlen(s1+1);
		l2=strlen(s2+1);
		dp[0][0]=1;
		if(s2[2]=='*') dp[2][0]=1;	  //!!
		for(j=1;j<=l2;++j){
//			if(s2[j]=='*'){
//				dp[j][0]=max(dp[j][0],dp[j-2][0]);
//			}	//开头没遍历过0
			for(i=1;i<=l1;++i){
				if(s1[i]==s2[j]||s2[j]=='.'){
					dp[j][i]=max(dp[j][i],dp[j-1][i-1]);
				}
				else if(s2[j]=='*'){
					dp[j][i]=dp[j-1][i]		//ab ab*
							 ||dp[j-1][i-1]&&s1[i]==s1[i-1]	   //abbbb ab* 第2个b
							 ||dp[j][i-1]&&(s2[j-1]==s1[i]||s1[i]==s1[i-1]&&s2[j-1]=='.')	  //abbbb ab* 第3个之后的b
							 ||dp[j-2][i];	   //abb abbb*
				}
			}
		}
		if(dp[l2][l1]) printf("yes\n");
		else printf("no\n");
    }

    return 0;
}

 

后来发现,这道题还有一种超级牛逼的做法:正则表达式。

题意和正则表达式一样啊嘤~

有点难理解,姑且看看大牛们的代码,如下:

#include 
#include 
#include 
#include 
using namespace std;

int main()
{
	ios::sync_with_stdio(false);	//关闭同步输出流
	int t;

	cin>>t;
	while(t--){
		string s1,s2;
		cin>>s1>>s2;
		s2=regex_replace(s2,regex("\\.\\*"),"(.)\\1*");		//因为正则表达式中的.*是可以代替任意长度任意字符而题目要求某一相同字符任意长度
		regex_match(s1,regex(s2))?cout<<"yes"<

 

你可能感兴趣的:(ACM,动态规划)