Binary String Matching STL秒解-不要太简单
2017-03-02 19:47
274 查看
在解决这道题之前先介绍一下string类find()的调用。
string str; int found;
str.find("needles are small",found+1,6);从found+1的位置开始
在str中查找"needles are small"字符串的前6个字符 也就是"needle"
str.find(str2) 在str中查找str2字符串 若找不到则返回string::npos
rfind() 类似,只是从反向查找
如要查找str2在str中出现多少次时 需要循环查找
found=s2.find(s1); 第一句find一定要写在循环外面 不然很难去控制while的结束条件
while(found!=string::npos)
{
count++;
found=s2.find(s1,found+1); 在上次查找的基础上加一位继续查找
}
一开始看以为是KMP的问题 后来发现string的STL就很容易解决问题直接上代码
其实很水到爆炸的题 我居然还在输入N后加了个getchar()导致CE了一次 也是无语。。。
string str; int found;
str.find("needles are small",found+1,6);从found+1的位置开始
在str中查找"needles are small"字符串的前6个字符 也就是"needle"
str.find(str2) 在str中查找str2字符串 若找不到则返回string::npos
rfind() 类似,只是从反向查找
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; int main() { int n;cin>>n; while(n--) { string s1,s2; cin>>s1>>s2; int found=0,count=0; found=s2.find(s1); while(found!=string::npos) { count++; found=s2.find(s1,found+1); } cout<<count<<endl; } }
如要查找str2在str中出现多少次时 需要循环查找
found=s2.find(s1); 第一句find一定要写在循环外面 不然很难去控制while的结束条件
while(found!=string::npos)
{
count++;
found=s2.find(s1,found+1); 在上次查找的基础上加一位继续查找
}
一开始看以为是KMP的问题 后来发现string的STL就很容易解决问题直接上代码
其实很水到爆炸的题 我居然还在输入N后加了个getchar()导致CE了一次 也是无语。。。
相关文章推荐
- Binary String Matching 5 (简单KMP模板题)
- NY--5 -- Binary String Matching [水] [STL] [string类]
- Binary String Matching 字符串的查找匹配 STL
- NYOJ Binary String Matching的stl解法 酒馆浪人的博客
- nyoj 5Binary String Matching
- nyoj-5-Binary String Matching
- Binary String Matching
- NYOJ 5 Binary String Matching
- nyoj5 Binary String Matching
- acm-Binary String Matching
- NYOJ 5-Binary String Matching
- Binary String Matching
- 【Binary String Matching 5】
- STL------string的简单使用
- acm每日一练之Binary String Matching
- Binary String Matching Java实现
- nyoj5 Binary String Matching
- Brute-Force算法 (eg.Binary String Matching)
- NYOJ5 Binary String Matching ——KMP
- Binary String Matching