substr()在c++的详细用法
substr()在头文件
#include<string>
中,格式为substr(a,b);
其中参数依次是 [开始,长度),并返回子串。
注意是左闭右开区间
注意第二个参数是长度,与其他语言不同
substr()里可以只传入"开始"这一个参数,默认长度为正无穷
应用实例:
给定两串字符串,求第一串字符串的后缀和第二串字符前缀相等的最小值。
输入:ababa baccc
输出:2
solution:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string a,b;
int main()
{
cin >> a >> b;
for(int k=1; k<min(a.size(), b.size()); k++)
if(a.substr(a.size()-k, k) == b.substr(0, k))
{
printf("%d", k);
break;
}
return 0;
}
浙公网安备 33010602011771号