牛客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;
}
发布了384 篇原创文章 · 获赞 65 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/Flynn_curry/article/details/104598890