POJ 1936 All in all 简单字符串比较

本文介绍了一种通过检查字符串s是否为字符串t的子序列的方法,使用C语言实现,并提供了两种不同的解决方案,一种是简单的字符匹配方法,另一种是采用动态规划的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一看到该题就认为可以用最长公共子序列来解
于是写了一下,提交显示MLE,改成循环数组又WA,不知道哪里问题
看到别人的解法,感觉思维方式很特别,很好,循环长串指针,比较长串和短串的一位,如果相等,短串指针加一
最后判断一下短串的指针有没有指到最后,如果移动到最后就说明查长串包含短串,否则.....
也提醒一下自己的思维定势
All in All
Time Limit:1000MS Memory Limit:30000K
Total Submissions:17955 Accepted:7127

Description

You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way. Because of pending patent issues we will not discuss in detail how the strings are generated and inserted into the original message. To validate your method, however, it is necessary to write a program that checks if the message is really encoded in the final string.

Given two strings s and t, you have to decide whether s is a subsequence of t, i.e. if you can remove characters from t such that the concatenation of the remaining characters is s.

Input

The input contains several testcases. Each is specified by two strings s, t of alphanumeric ASCII characters separated by whitespace.The length of s and t will no more than 100000.

Output

For each test case output "Yes", if s is a subsequence of t,otherwise output "No".

Sample Input

sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter

Sample Output

Yes
No
Yes
No
/* Author yan
 * POJ 1936
 * All in all
*/
#include<stdio.h>
#define MAX 100000
char str1[MAX],str2[MAX];

int main()
{
	int i,j;
	int len1,len2;
	//freopen("input","r",stdin);
	while(scanf("%s %s",str1,str2)!=EOF)
	{
		len1=strlen(str1);
		len2=strlen(str2);
		j=0;
		for(i=0;i<len2;i++)
		{
			if(str2[i]==str1[j]) j++;
		}
		if(j==len1) printf("Yes/n");
		else printf("No/n");
	}
	return 0;
}
LCS  AC!!!!
刚刚开始用循环数组时低维不应该循环,因为低维的以后会用到,因为是两层循环!!标记!!!
/* Author yan
 * POJ 1936
 * All in all
*/
#include<stdio.h>
#define MAX 100000
int opt[2][MAX];
char str1[MAX],str2[MAX];

int main()
{
	int i,j;
	int len1,len2;
	//freopen("input","r",stdin);
	while(scanf("%s %s",str1,str2)!=EOF)
	{
		len1=strlen(str1);
		len2=strlen(str2);
		memset(opt,0,sizeof(opt));
		for(i=1;i<=len1;i++)
			for(j=1;j<=len2;j++)
			{
				if(str1[i-1]==str2[j-1]) opt[i%2][j]=opt[(i-1)%2][j-1]+1;
				else if(opt[i%2][j-1]>=opt[(i-1)%2][j]) opt[i%2][j]=opt[i%2][j-1];
				else opt[i%2][j]=opt[(i-1)%2][j];
			}
		if(opt[len1%2][len2]==len1) printf("Yes/n");
		else printf("No/n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值