按如下规则转换字母:
Let's consider that a word has been typed with the Caps lock key accidentally switched on, if:
-
either it only contains uppercase letters;
-
or all letters except for the first one are uppercase.
In this case we should automatically change the case of all letters. For example, the case of the letters that form words "hELLO", "HTTP",
"z" should be changed.
单个大写字母是否需要转换?需要。
cBCD -> Cbcd
Good -> Good
H -> h
h -> H
看似乎简单,但是解决这个问题可以,要优雅地写出程序,那么就是不容易的了。
下面看我的程序,使用automata的知识,写个小小的automata系统,优雅地解决这个问题:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
enum CASE
{
ALL_UPPERS,
ONLY_FIRST_LOWER,
CORRECT,
ILEGAL
};
const static int trans[6][2] = {
{1, 2}, //0 Init and Invalid
{3, 5}, //1 first upper
{4, 5}, //2 first lower
{3, 5}, //3 all upper
{4, 5}, //4 first low other upper
{5, 5} }; //5 correct words
CASE checkLetters(string &s)
{
int state = 0;
for (int i = 0; i < s.size(); i++)
{
int input = -1;
if ('A' <= s[i] && s[i] <= 'Z') input = 0;
if ('a' <= s[i] && s[i] <= 'z') input = 1;
if (-1 == input) return ILEGAL;
state = trans[state][input];
}
if (1 == state || 3 == state) return ALL_UPPERS;
else if (2 == state || 4 == state) return ONLY_FIRST_LOWER;
return CORRECT;
}
void transAllLowers(string &s)
{
for (int i = 0; i < s.size(); i++)
{
s[i] = s[i] - 'A' + 'a';
}
}
void transFirstUpper(string &s)
{
s[0] = s[0] - 'a' + 'A';
for (int i = 1; i < s.size(); i++)
{
s[i] = s[i] - 'A' + 'a';
}
}
void cAPSLOOCK()
{
string s;
cin>>s;
CASE C = checkLetters(s);
if (ALL_UPPERS == C) transAllLowers(s);
else if (ONLY_FIRST_LOWER == C) transFirstUpper(s);
else if (ILEGAL == C) cout<<"ILEGAL", exit(0);
cout<<s;
}
int main()
{
cAPSLOOCK();
return 0;
}
分享到:
相关推荐
Codeforces - 1107B. Digital root & 1107C. Brutality(规律 & 贪心)Codeforces - 1107B.
codeforces-ACM竞赛题目-2833道.tgz
Codeforces - 1131C. Birthday(贪心)题目链接题目给你n和n个数,要你重新排列n个数,使得这些数的相邻差值中最大的那个值最小。stat
Codeforces 149 D-Coloring Brackets,动态规划求解
codeforces-cli 在python和bash中使用简单的解析和检查实现,以解决Codeforce中的竞赛。\ 安装 git clone " https://github.com/siddharth17196/codeforces-cli.git " cd codeforces-cli/scripts bash install.sh ...
Codeforces-Problem-Set
codeforces-js Codeforces JS
git clone " https://github.com/ahmmedabir9/codeforces-contest-template.git "cd codeforces-contest-templatenpm install # install the dependencies from package.json用法 export CF_CONTEST=...
codeforces-plugin-sublime-text 注意:仅适用于 C++ 代码。 它能做什么? 编译你的 C++ 代码,构建它,获取测试用例,在上面运行你的代码,比较输出。 它不做什么: 通过互联网神奇地识别您的问题并进行判断。 ...
Codeforces-cpp-Code
Codeforces-Newcomer-Sheet
Codeforces-problem-solution
Codeforces-Ladder-A
Codeforces-Round-492-:解决问题shorturl.atetAIW
codeforces-stats-analyzer 可以分析用户个人资料
leetcode卡Codeforces 统计 一个工具可以帮助您显示 CF 统计信息。 灵感来自 . 范围 范围 默认 必需的 处理 空值 真的 贡献 错误的 错误的 朋友们 错误的 错误的 主题 默认 错误的 主题选择:默认、黑暗 例子 默认 ...
CP-Codeforces-Stats
codeforces-go :thought_balloon: :light_bulb: :balloon:算法我已经在实现了一些算法。如何练习从难度等于(“您的等级” + 200)的问题开始,您将要解决对您来说非常困难的问题。 花费不超过10分钟,然后再查看...
Codeforces-1497A-Mimimization链接: :
Codeforces-177A1-良好矩阵元素 链接: :