C++ string字符串截取

很久没写题连string字符串的截取都忘了;

附上

#include<bits/stdc++.h>
using namespace std;

int main(){
	string s("abaacd");
	string res=s.substr(1,4);//从第一个位置截取4个字符串
	cout<<res<<endl; 
	//输出 baac 
	return 0;
} 

再附上一题http://codeforces.com/contest/1029/problem/A感觉自己写的挺麻烦的饿。。

#include<iostream>
#include<string>
#include<limits.h>
using namespace std;
typedef long long ll;
const ll INF=LONG_LONG_MAX; 
int k,t;
string s;
int main(){
	while(cin>>k>>t>>s){
		string res=s;
		string temp;
		int num;
		int i,j;
		for(i=1;i<s.size();++i){
			if(s[i]==s[0]){
//				cout<<"i: "<<i<<endl;
				num=0;
				for(j=i;j<s.size();++j){
					if(s[num]!=s[j]){
						break;
					}
					num++;
				}
				if(j>=s.size()){
					break;
				}
			}
		}
		if(i>=s.size()){
			temp=s;
			for(int k=0;k<t-1;++k){
				res+=temp;
			}
			cout<<res<<endl;
		}
		else{
			temp=s.substr(num,s.size()-num);
//			cout<<temp<<endl;
			for(int k=0;k<t-1;++k){
				res+=temp;
			}
			cout<<res<<endl;
		}
		res.clear();
		temp.clear();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/TDD_Master/article/details/82423793