首页 > 其他分享 >abc286

abc286

时间:2023-01-23 12:22:08浏览次数:48  
标签:string 获取 pos substr str 字符串 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;
}

标签:string,获取,pos,substr,str,字符串,abc286
From: https://www.cnblogs.com/csai-H/p/17065104.html

相关文章

  • ABC286 上分记 & 解题报告
    AtcoderBeginnerContest286contestlinkcontestresult解题记录留坑待填......
  • AT_abc286d 题解
    板子首先我们看到值域并不大。因此可以维护值域,跑完全背包。具体而言维护某一个值(小于\(10000\))是否能被凑出来,然后枚举物品种类以及物品数量即可。一般而言,完全背包......
  • AT_abc286e 题解
    首先观察到\(n\leq300\)加上全源“最短路”便可以自然而然的想到floyd。注意到floyd算法的可行性只依赖统计的东西具有优先级。这里我们定义优先级为最短路最短且......
  • ABC286 A-E题解
    题目虽然是大年三十,但是玩手机没写题有意思。从50分钟才开始看题。A题意:将数组中\([p,q]\)与\([r,s]\)的元素交换并输出。sbt。B大意:将字符中的na换成nya。......