统一命名规范
统一命名规范
https://ac.nowcoder.com/acm/contest/80917/F
统一命名规范
标签: 字符串 集合
难度: 适中
思路:
先根据命名规则将单词划分出来,然后再根据命名规则重新命名。
如果只有一个单词,Camel
和 Snake
的写法一致,而 Pascal
首字母大写。
如果但系数量很多, Camel
和 Pascal
根据后面的大写字母划分单词,Snake
则根据后面的下划线划分单词。
#include<bits/stdc++.h>
using namespace std;
int n;
string type;
//获取单词
vector<string> GetWords(const string &str)
{
vector<string>res; //单词容器
string temp;
for(char ch:str)
{
//遇到大写或者'_' 停止,判断,计入,清理
if(isupper(ch)||ch=='_')
{
if(!temp.empty())
{
res.push_back(temp);
temp.clear();
}
}
//遇到字母变小写后计入temp
if(ch!='_')
temp+=tolower(ch);
}
//特别考虑最后一个单词的计入
if(!temp.empty())
res.push_back(temp);
return res;
}
//"Camel"表示转换
string Camel(const vector<string> &words)
{
string res;
res+=words.front();
for(int i=1;i<words.size();i++)
{
string word=words[i];
word[0]=toupper(word[0]);
res+=word;
}
return res;
}
//“Pascal”表示转换
string Pascal(const vector<string> &words)
{
string res;
for(int i=0;i<words.size();i++)
{
string word=words[i];
word[0]=toupper(word[0]);
res+=word;
}
return res;
}
//"Snake"表示转换
string Snake(const vector<string> &words)
{
string res;
res+=words.front();
for(int i=1;i<words.size();i++)
{
res+="_"+words[i];
}
return res;
}
int main()
{
cin>>n>>type;
for(int i=1;i<=n;i++)
{
string str;
cin>>str;
vector<string>words=GetWords(str);
if(type=="Camel")
cout<<Camel(words)<<endl;
else if(type=="Pascal")
cout<<Pascal(words)<<endl;
else
cout<<Snake(words)<<endl;
}
return 0;
}