pri*_*sia 6 string algorithm data-structures
给出像bangalore和blr这样的2个字符串,返回一个是否作为另一个的子序列出现.上述情况返回true,而bangalore和brl返回false.
das*_*ght 17
贪婪的策略应该适用于这个问题.
以下是C++中的示例代码:
#include <iostream>
#include <string>
using namespace std;
int main() {
string txt = "quick brown fox jumps over the lazy dog";
string s = "brownfoxzdog";
int pos = -1;
bool ok = true;
for (int i = 0 ; ok && i != s.size() ; i++) {
ok = (pos = txt.find(s[i], pos+1)) != string::npos;
}
cerr << (ok ? "Found" : "Not found") << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7696 次 |
| 最近记录: |