poj 1936 串(字序列匹配,满足贪心性质)复杂度O(n+m)
来源:互联网 发布:js vr 插件 编辑:程序博客网 时间:2024/06/05 16:30
All in All
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 26071 Accepted: 10570
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.
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 subsequenceperson compressionVERDI vivaVittorioEmanueleReDiItaliacaseDoesMatter CaseDoesMatter
Sample Output
YesNoYesNo
Source
Ulm Local 2002
水题。其实就是poj3267的一段子程序拿来又出了一道。
基本思想用贪心。
两个指针,i指向模式串,j指向母串。j一直往后扫,一旦发现与i指向的字符相同,i++。
这样当i走完模式串,也就说明模式串被匹配到了。
这里大家可能会困惑,如果有多个匹配,这样求得的是哪个匹配呢?
答案应该是,所有匹配到的字符 都尽可能排在前面的 那种情况。换句话说,只要匹配到了,i就可以++,无需考虑后面的。是满足贪心性质的。
这个与连续字串匹配是不同的:
例如:
在abzdabc中,如果需要匹配子序列abc,则会匹配到abzdabc. 只需要直接扫即可。并不需要kmp之类的优化。
而如果在abzdabc中,匹配子串abc,则只能匹配到abzdabc. 不具有贪心性质。暴力只能用o(n*m)复杂度,当然用kmp可以优化到O(n+m)。
这段子程序很经典,感受一下:
for (i = 0, j = 0; i < lens, j < lent; j++) { if (s[i] == t[j]) i++;}if (i == lens) cout << "Yes" << endl;else cout << "No" << endl;
提交记录:
1.RE 。。。居然是因为数组开小了。人家是100000, 我开了10000
2.Accepted!
- poj 1936 串(字序列匹配,满足贪心性质)复杂度O(n+m)
- KMP 算法。。。 字符串匹配 ,复杂度 O(n+m)
- 最长公共字序列 动态规划 空间复杂度o(min{n,m})带打印
- poj 2127 时间复杂度为O(m*n)
- poj 1458 dp [最长公共子序列 O(n*m)]
- 时间复杂度为O(m*n)最长公共子串
- 字符串模式匹配——KMP算法(时间复杂度为O(m+n))
- [算法] 匈牙利算法 poj 1274 示例 [ 二分图匹配 入门篇 ] O(n*m) Hopcroft O(sqrt(n)*m)
- 字符串左旋满足时间复杂度为O(n)
- 将m,n(m>n)的连续自然数序列,打乱顺序重新排列,随机取出一个数,试问取出的这个数是多少?使用一种解法,时间复杂度O(m-n),空间复杂度O(1)
- 约瑟夫问题,从o(n*m)到o(n)乃至o(m)的算法复杂度进阶
- KMP(字符串匹配)算法 O(m+n)
- poj 3061 时间复杂度为O(n)
- 时间复杂度为m+n,的一种模式匹配,适合子串不是很长的匹配
- 以时间复杂度O(n)计算最大子序列和
- 最长有序子序列 时间复杂度O(n^2)
- 最大连续子序列和问题O(N)复杂度
- POJ 2085 treap O(nlogn) 与 贪心 O(n)算法
- 浅淡JAVA异常机制如何设计
- openNTF Project Introduction - JavaScript Un-obfuscation Tool for IBM iNotes
- The reference to entity "autoReconnect" must end with the ';' delimiter.
- Java文件操作:按行读取和写入
- android打印调用关系的方法
- poj 1936 串(字序列匹配,满足贪心性质)复杂度O(n+m)
- opencv支持ffmpeg,pkg-config的使用注意事项
- (基础学习)MySQL体系结构
- MoveWindow和setwindowpos的区别
- XMLHttpRequest对象的属性和方法
- MyEclipse插件介绍与下载
- 线性表-双链表
- 用ci框架遇到的错误一
- Uninstall 7-zip with WindowsUninstaller.Org Removal Tips