abc286

C(string类 substr用法)

substr函数的形式:

s.substr(pos, n);

参数:

需要两个参数,第一个是开始位置,第二个是获取子串的长度。
函数可以从一个字符串中获取子串,返回一个string,包含s中从pos开始的n个字符的拷贝(pos的默认值是0,n的默认值是s.size() - pos,即不加参数会默认拷贝整个s)

两个用法

string str.substr(pos)	//默认从str字符串pos位置开始截取到str结束为止
string str.substr(pos, n)	//若pos的值超过了string的大小,则substr函数会抛出一个out_of_range异常
				//若pos+n的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾

举例

	string str("123456abcd");
	string s1 = str.substr(2, 3);	//获取字符串str中,从第二个位置'3'开始,长度为3的字符串 s1为:345
	string s2 = str.substr(3);	//获取字符串str中,从第三个位置'4'开始到结束的字符串,s2为:456abcd
	string s3 = str.substr(12);	//能过语法,但是没用
	string s4 = str.substr(2, 12);	//获取字符串str中,从第二个位置'3'开始到结束的字符串(因为想要获取子串长度12大于str大小),s4为:3456abcd
#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
using namespace std;
typedef long long LL;
const double INFF = 0x7f7f7f7f7f7f7f7f;

LL n, a, b;
string s;
int main() {
	ios;
	cin >> n >> a >> b >> s;
	s = " " + s;
	LL ans = INFF;
	for (int i = 0; i <= n; i++) {
		LL now = a * i;
		string ss = s.substr(i + 1) + s.substr(1, i);
		ss = " " + ss;
		for (int j = 1, k = n; j < k; j++, k--) {
			if (ss[j] != ss[k]) {
				now += b;
			}
		}
		ans = min(ans, now);
	}
	cout << ans;
	return 0;
}

原文链接: https://www.cnblogs.com/csai-H/p/17065104.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    abc286

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/313709

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 下午1:00
下一篇 2023年2月16日 下午1:01

相关推荐