P5733 【深基6.例1】自动修正

image-20240514200940300

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include<string>
using namespace std;

int main()
{

string a;
getline(cin, a);
for (char& tmp : a)
{
tmp = toupper(tmp);
}
cout << a;
}

P1914 小书童——凯撒密码

image-20240514201119286

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include<string>
using namespace std;

int main()
{

string a;
int mv;
cin >> mv;
cin.ignore();
getline(cin, a);
for (char& tmp : a)
{
tmp =(tmp - 'a' + mv)%26 + 'a';
}
cout << a;
}

P1125 [NOIP2008 提高组] 笨小猴

image-20240514201203246

经典看错题目,没注意到No Answer时输出0。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include<string>
using namespace std;
const int N = 30;
int arr[N];

int main()
{
int max = 0, min = 10000;

string a;
getline(cin, a);
for (char& tmp : a)
{
arr[tmp - 'a']++;
}


for (int i = 0; i < 26; i++)
{
if (max < arr[i])
{
max = arr[i];
}
if (min > arr[i] && arr[i] != 0)
{
min = arr[i];
}
}

bool flag = true;
int res = max - min;

if (res == 1 || res == 0) flag = false;
else {
for (int i = 2; i*i <= res; i++)
if (res % i == 0) {
flag = false;
}
}


if (flag)
{
cout << "Lucky Word" << endl;
cout << res << endl;
}
else
{
cout << "No Answer" << endl;
cout << "0" << endl;
}

}

P1957 口算练习题

image-20240514230917944

这里我们需要对输出计算长度,因此采用cstdiosprintf将格式化输出写入一个字符串,然后计算字符串长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include<cstdio> //sprintf
#include<cstring> //strlen
using namespace std;

int fuhao;
int i, j;

void print()
{
char buffer[100];
switch (fuhao)
{
case 'a':
sprintf(buffer,"%d+%d=%d", i, j, i + j);
break;
case 'b':
sprintf(buffer, "%d-%d=%d", i, j, i - j);
break;
case 'c':
sprintf(buffer, "%d*%d=%d", i, j, i * j);
break;
}
int len = strlen(buffer);
cout << buffer << endl << len << endl;

}

int main()
{
int num;
cin >> num;

while (num--)
{
char ss[20]; //这里采用字符串的原因是输入整数可能大于255
scanf("%s",ss);
if (isalpha(ss[0])) //注意isalpha参数要求为char
{
fuhao = ss[0];
cin >> i >> j;
print();
}
else
{
i = atoi(ss);
cin >> j;
print();
}
}

return 0;
}

P5015 [NOIP2018 普及组] 标题统计

image-20240514232058050

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include<string>

using namespace std;

int main()
{
string str;
getline(cin, str);
int count = 0;

for (const char& ch : str)
{
if (isalnum(ch)) count++;
}
cout << count << endl;

return 0;
}

P5734 【深基6.例6】文字处理软件

image-20240515124114479

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

#include <iostream>
#include<string>

using namespace std;

int main()
{
int num;
cin >> num;
string str;
cin >> str;
string tmpstr;
int a, b, pos;
while (num--)
{
int tmp;

cin >> tmp;
switch (tmp)
{
case 1:
{

cin >> tmpstr;
str += tmpstr;
cout << str << endl;
break;
}
case 2:
{

cin >> a >> b;
str = str.substr(a, b);
cout << str << endl;
break;
}
case 3:
{


cin >> pos >> tmpstr;
str.insert(pos, tmpstr);
cout << str << endl;
break;
}
case 4:
{

cin >> tmpstr;

if ((pos = str.find(tmpstr)) == string::npos)
cout << "-1" << endl;
else
cout << pos << endl;
break;
}
}
}

return 0;
}

P1308 [NOIP2011 普及组] 统计单词数

image-20240515230015899

这里由于不区分大小写,我们可以将字符串都转化为小写进行比较,这里使用algorithmstd::transform。然后逐步查找字符串。考虑到查找的是单个单词,我们在源字符串中插入空格,这样我们只需在匹配字符串中左右加入空格即可完成匹配。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include<string>
#include<algorithm>

using namespace std;

int main()
{
string s1, s2;

cin >> s1;
cin.ignore();
getline(cin,s2);
//
transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
transform(s2.begin(), s2.end(), s2.begin(), ::tolower);

s2.insert(0,1, ' '); //在0插入1个空格
s2 += ' ';
s1.insert(0, 1, ' '); //在0插入1个空格
s1 += ' ';

if (s2.find(s1) == string::npos)
{
cout << "-1";
}
else
{
int count = 0;
int pos = 0;
while ((pos = s2.find(s1,pos)) != string::npos)
{
count++;
pos += s1.length() - 2; // 由于pos是字符串开始的位置,我们可以加上字符串长度(注意要减两个空格),以减少查找范围
}
cout << count << " " << s2.find(s1) << endl;
}


return 0;
}

P1765 手机

这里我们使用unordered_map来实现键值对的查找。题解中有用输入减去a取数组对应的值,相对更简便写。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

unordered_map<char, int> map =
{
{'a', 1}, {'b', 2}, {'c', 3}, {'d', 1}, {'e', 2}, {'f', 3},
{'g', 1}, {'h', 2}, {'i', 3}, {'j', 1}, {'k', 2}, {'l', 3},
{'m', 1}, {'n', 2}, {'o', 3}, {'p', 1}, {'q', 2}, {'r', 3},
{'s', 4}, {'t', 1}, {'u', 2}, {'v', 3}, {'w', 1}, {'x', 2},
{'y', 3}, {'z', 4}, {' ', 1}
};

int main()
{
string s1;
int count = 0;

getline(cin, s1);

for (const char& tmp : s1)
{
if (map.find(tmp) != map.end())
{
count += map[tmp];
}
}

cout << count;

return 0;
}

P3741 小果的键盘

image-20240516170245784

这题能达成KV条件有两种方式:

  1. 原字符串就是KV
  2. 通过修改KKVV来产生一个KV,这里KKVV不能是原字符串的一部分,如:KKV若修改为KVV,原字符串数量反而未增加。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<string>
using namespace std;

int main()
{
int n;
string s;
int pos = 0;
int count = 0;
cin >> n >> s;

while ((pos = s.find("VK", pos)) != string::npos)
{
s[pos] = 'T';
s[pos + 1] = 'T';
pos += 2;
count++;

}

if (s.find("KK") != string::npos || s.find("VV") != string::npos)
{

cout << ++count << endl;
return 0;
}


cout << count << endl;

return 0;
}

P1321 单词覆盖还原

image-20240516180146447

这题我原本思路是计算各个字符被遮盖的次数,但是怎么可能每次遮盖的都是该字符呢。。。

我们可以采用找该字符串特征值的方式:我们知道,虽然boygirl互相被遮挡,但是字符串中的顺序是不会变的,而且两字符串无重复值,只需检测出一个字符符合序列即可判定。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<string>
using namespace std;
int main()
{
string in, boy = "boy", girl = "girl";

int boycount = 0, girlcount = 0;

cin >> in;

for (int i = 0; i < in.length() - 2; i++)
if (in[i] == boy[0] || in[i + 1] == boy[1] || in[i + 2] == boy[2])
boycount++;
for (int i = 0; i < in.length() - 3; i++)
if (in[i] == girl[0] || in[i + 1] == girl[1] || in[i + 2] == girl[2] || in[i + 3] == girl[3])
girlcount++;
cout << boycount << endl << girlcount << endl;

}

P1553 数字反转(升级版)

image-20240516180425589

这里使用string库中的erase() reverse() find_first_not_of() find_last_not_of()这几个stl函数来实现.

这里要注意的是小数部分前面是可以有0的.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<string>

using namespace std;


string Reverse_String(string s) {
reverse(s.begin(), s.end());
s.erase(0, s.find_first_not_of('0')); // 移除前导'0'
return s.empty() ? "0" : s;
}

string Tail(string s) {
s.erase(s.find_last_not_of('0') + 1); // 移除尾随'0'
return s.empty() ? "0" : s;
}


int main()
{
string s ,res;
cin >> s;
int pos;
if ((pos = s.find('.')) != string::npos)
{
res = Reverse_String(s.substr(0, pos));
res += '.';
res += Tail(Reverse_String(s.substr(pos + 1)));
}
else if ((pos = s.find('%')) != string::npos)
{
res = Reverse_String(s.substr(0, s.length() - 1));
res += '%';
}
else if ((pos = s.find('/')) != string::npos)
{
res = Reverse_String(s.substr(0, pos));
res += '/';
res += Reverse_String(s.substr(pos + 1));
}
else
{
res = Reverse_String(s);
}
cout << res << endl;
return 0;
}

P1603 斯诺登的密码

我们用unordered_map来表示对应关系,注意当只有一位数且该数不为首位时,我们在前面添加0。

image-20240517115038119

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;

unordered_map<string, int> map = {
{"one", 1}, {"two", 2}, {"three", 3}, {"four", 4}, {"five", 5},
{"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9}, {"ten", 10},
{"eleven", 11}, {"twelve", 12}, {"thirteen", 13}, {"fourteen", 14}, {"fifteen", 15},
{"sixteen", 16}, {"seventeen", 17}, {"eighteen", 18}, {"nineteen", 19}, {"twenty", 20},
{"a", 1},
{"both", 2},
{"another", 1},
{"first", 1},
{"second", 2},
{"third", 3},
};

int main() {
string str;
vector<int> res;
for (int i = 0; i < 6; i++) {
cin >> str;
if (map.find(str) != map.end()) {
int num = map[str];
num = (num * num) % 100;
res.push_back(num);
}
}
sort(res.begin(), res.end());

if (res.empty()) {
cout << 0 << endl;
return 0;
}

cout << res[0];
for (int i = 1; i < res.size(); i++) {
if (res[i] < 10) {
cout << 0 << res[i];
} else {
cout << res[i];
}
}
cout << endl;

return 0;
}

P1200 [USACO1.1] 你的飞碟在这儿 Your Ride Is Here

image-20240517120603409

这里的话,要注意下转换时(a[i] - 'A' + 1)要加1,否则答案不对。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<string>

using namespace std;

int main()
{
string a, b;
cin >> a;
cin >> b;
int a_num = 1, b_num = 1;

for (int i = 0; i < a.length(); i++)
{
a_num *= int(a[i] - 'A' + 1);
}
a_num = a_num % 47;

for (int i = 0; i < b.length(); i++)
{
b_num *= int(b[i] - 'A' + 1);
}
b_num = b_num % 47;

if (b_num == a_num)
cout << "GO" << endl;
else
cout << "STAY" << endl;
return 0;
}

P1597 语句解析

image-20240517125243119

这题给我cpu干烧了,脑子没反应过来,把a赋值给b不是让b的值为a。那么这题则可以使用map来解决。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

int main() {

unordered_map<char, int> var = { {'a', 0}, {'b', 0}, {'c', 0} };
string input;
getline(cin, input);


int pos = 0;
while (pos < input.size()) {
char var1 = input[pos];
pos += 3;
char var2 = input[pos];
pos += 2;

if (isdigit(var2)) {
var[var1] = var2 - '0';
}
else {
var[var1] = var[var2];
}
}

cout << var['a'] << " " << var['b'] << " " << var['c'] << endl;

return 0;
}

P1598 垂直柱状图

image-20240517131831636

这题还是比较简单的,要注意下判断该字符是否为字符(原文有空格和句号)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
using namespace std;
int countnum[30] = { 0 };

int main()
{
string s;
for (int i = 0; i < 4; i++)
{
getline(cin, s);
for (const char& ch : s)
{
if(isalpha(ch))
countnum[ch -'A'] ++ ; //原本想命名为count,发现count时std命名空间了的。。。
}
}


while (1)
{
int maxnum = 0;
for (int i = 0; i < 26; i++)
{
if (countnum[i] > maxnum)
maxnum = countnum[i];
}
if (maxnum)
{
for (int i = 0; i < 26; i++)
{
if (countnum[i] == maxnum)
{
cout << "*" << " ";
countnum[i]--;
}

else
cout << " ";
}
cout << endl;
}
else
{
break;
}

}
for (int i = 0; i < 26; i++)
{
cout << char('A' + i) << " ";
}

}

洛谷字符串完结撒花!!!