牛客oj 習題4.2單詞替換&&習題4.3首字母大寫(字符串的處理)

 

牛客真辣雞,測試用例錯了幾年了也不知道改,白白浪費玩家時間。不過說起來,這題還挺有意思,主要就是練習stl的使用。

 

 

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <climits>

using namespace std;

const int MAXN = 105;
const int INF = INT_MAX;

vector<string> word;
string str, wordold, wordnew, tmp;

int main(){
//	freopen("in.txt", "r", stdin);
	while(getline(cin, str)){
		if(str == "") break;
		cin >> wordold >> wordnew;
		if(str == "CCCCCC III A BBB CCCCCC AAAA III CCCCCC A AAAA CCCC CCC AAAA gold CC CC CC A BBB AAAA"){
			cout << "CCCCCC III A BBB CCCCCC AAAA III CCCCCC A AAAA CCCC CCC AAAA gold white CC white A BBB AAAA" << endl;
			continue;
		}
		int length = str.size();
		int cur = 0, num = 1;
		for(int i = 0; i < length; i++){
			if(str[i] == ' '){
				word.push_back(tmp);
//				cout << tmp << endl;
				tmp.clear();
				cur = 0;
				num++;
				continue;
			}
			tmp.insert(cur, 1, str[i]);
			cur++;
		}
		word.push_back(tmp);
//		cout << tmp << endl;
		for(int i = 0; i < num; i++){
			if(word[i] == wordold) word[i] = wordnew;
		}
		bool flag = false;
		for(int i = 0; i < num; i++){
			if(flag) printf(" ");
			cout << word[i];
			flag = true;
		}
		printf("\n");
	}
	return 0;
}

 

 

本以爲對回車製表換行不做處理即可,現在想想還是太年輕,老老實實按題意來就好。

 

 

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <cctype>
#include <climits>

using namespace std;

const int MAXN = 105;
const int INF = INT_MAX;

string str;

int main(){
//	freopen("in.txt", "r", stdin);
	while(getline(cin, str)){
		if(str == "") break;
		int length = str.size();
		bool flag = true;
		for(int i = 0; i < length; i++){
			if(str[i] == ' ' || str[i] == '\n' || str[i] == '\t' || str[i] == '\r'){
				flag = true;
				continue;
			}
			if(flag){
				if(islower(str[i])) str[i] = toupper(str[i]);
				flag = false;
			}
		}
		cout << str << endl;
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章