1、第一个只出现一次的字符:在字符串中找出第一个只出现一次的字符。如输入“abaccdeff”则输出b
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
#define length(a) ( (sizeof(a)) / (sizeof(a[0])) )
int main()
{
string str;
cin >> str;
int len = str.size();
unsigned int ch[256] = { 0 };
for (int i = 0; i < len; i++) {
ch[str[i]]++;
}
int j = 0;
while (len--) {
if (ch[str[j]] == 1) {
cout << str[j] << endl;
break;
}
j++;
}
return 0;
}
2、翻转单词顺序VS左旋转字符串:题目一:输入一个英语句子,翻转句子中单词的顺序,但是单词的字符的顺序不变。变电符号和普通字符一样处理。
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
using namespace std;
#define length(a) ( (sizeof(a)) / (sizeof(a[0])) )
int main()
{
vector<string> result;
vector<int> pos;
string str;
// cin >> str;
// 输入整行
getline(cin, str);
int len = str.size();
// 存储空格所在位置
pos.push_back(0);
for (int i = 0; i < len; i++) {
if (str[i] == ' ')
pos.push_back(i);
}
pos.push_back(len);
for (int i = pos.size(); i > 2; i--) {
//cout << pos[i - 1] << " ";
cout << str.substr(pos[i - 2] + 1, pos[i - 1] - pos[i - 2] -1) << " ";
}
cout << str.substr(0, pos[1]);
cout << endl;
cout << "length of pos = " << pos.size() << endl;
return 0;
}
3、编码实现求给定字符串(全为小写英文字母)的最小后继,如”abc”的最小后继为”abd”,”dhz”的最小后继为”di”。(Google笔试题)
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
using namespace std;
#define length(a) ( (sizeof(a)) / (sizeof(a[0])) )
int main()
{
string str1;
string str2;
cin >> str1;
for (int i = str1.size() - 1; i >= 0; i--) {
if (str1[i] + 1 <= 'z') {
str1[i] += 1;
str1[i + 1] = '\0';
break;
}
}
cout << str1 << endl;
return 0;
}