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;
}
posted @ 2023-01-23 12:09  csai_H  阅读(47)  评论(0)    收藏  举报