(原文地址:https://www.cnblogs.com/wkfvawl/p/9429128.html)
1.string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos。(返回值可以看成是一个int型的数)
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
find函数返回类型 size_type
string s("1a2b3c4d5e6f7jkg8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position;
//find 函数 返回jk 在s 中的下标位置
position = s.find("jk");
if (position != s.npos) //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,
{
printf("position is : %d\n" ,position);
}
else
{
printf("Not found the flag\n");
}
}
运行结果:
2.返回子串出现在母串中的首次出现的位置,和最后一次出现的位置。
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
find函数返回类型 size_type
string s("1a2b3c4d5e6f7jkg8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position;
flag = "c";
position = s.find_first_of(flag);
printf("s.find_first_of(flag) is :%d\n", position);
position = s.find_last_of(flag);
printf("s.find_last_of(flag) is :%d\n", position);
}
运行结果:
3.查找某一给定位置后的子串的位置
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
find函数返回类型 size_type
string s("1a2b3c4d5e6f7jkg8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position;
//从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标
position = s.find("b", 5);
cout << "s.find(b,5) is : " << position << endl;
}
运行结果:
4.查找所有子串在母串中出现的位置
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
find函数返回类型 size_type
string s("1a2b3c4d5e6f7jkg8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position;
//查找s 中flag 出现的所有位置。
flag = "a";
position = 0;
int i = 1;
while ((position = s.find(flag, position)) != string::npos)
{
cout << "position " << i << " : " << position << endl;
position++;
i++;
}
}
运行结果:
5.反向查找子串在母串中出现的位置,通常我们可以这样来使用,当正向查找与反向查找得到的位置不相同说明子串不唯一。
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
find函数返回类型 size_type
string s("1a2b3c4d5e6f7jkg8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position;
//反向查找,flag 在s 中最后出现的位置
flag = "3";
position = s.rfind(flag);
printf("s.rfind (flag) :%d\n", position);
return 0;
}
运行结果:
例题:
1.给出一个字符串,串中会出现有人名,找到一个只有一个人名的字符串。
//#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#include <vector>
vector<string> s;
int main()
{
s.push_back("Danil");
s.push_back("Olya");
s.push_back("Slava");
s.push_back("Ann");
s.push_back("Nikita");///建立动态数组
string a;
cin >> a;
int res = 0;
for (int i = 0; i < 5; i++)
{
if (a.find(s[i]) != a.npos)
{
res++;
if (a.rfind(s[i]) != a.find(s[i]))///一个字符中出现多个一样的名字
{
res++;
}
}
}
if (res == 1)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
return 0;
}
2.你有n个字符串。 每个字符串由小写英文字母组成。 重新排序给定的字符串,使得对于每个字符串,在它之前的所有字符串都是它的子串。
https://www.cnblogs.com/wkfvawl/p/9229758.html
#include<string>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
bool cmp(string a, string b)
{
if (a.length() == b.length())
return a < b;
return a.length() < b.length();
}
int main()
{
int n;
string s[111];
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> s[i];
}
sort(s, s + n, cmp);
int flag = 1;
for (int i = 1; i < n; i++)
{
if (s[i].find(s[i - 1]) == string::npos)
{
flag = 0;
break;
}
}
if (flag)
{
cout << "YES" << endl;
for (int i = 0; i < n; i++)
{
cout << s[i] << endl;
}
}
else
{
cout << "NO" << endl;
}
return 0;
}
3.查询区间内子串在母串中的个数。
https://www.cnblogs.com/wkfvawl/p/9452869.html
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n, m, q, i, j, l, r, len;
int counts;
int vis[10010];
string s1, s2;
cin >> n >> m >> q;
cin >> s1 >> s2;
len = s2.size();
memset(vis, 0, sizeof(vis));
string::size_type pos = 0;
while ((pos = s1.find(s2, pos)) != string::npos)
{
vis[pos + 1] = pos + 1;
pos++;
}
for (i = 1; i <= q; i++)
{
counts = 0;
//scanf("%d%d", &l, &r);
cin >> l >> r;
for (j = l; j <= r; j++)
{
if (vis[j] != 0 && vis[j] + len - 1 <= r)
{
counts++;
}
}
//printf("%d\n", counts);
cout << counts << endl;
}
return 0;
}