实训week3:字符串专题

和上章比起来还是简单了不少2333
sstream从某种意义上可以实现split

练习题目:

EOJ 3000

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
#include <iostream>
using namespace std;
void trans(string& str) {
for(int i = 0;i < str.size();i++){
if(str[i] >= 'A' && str[i] <= 'M')
str[i] += 13;
else if(str[i] >= 'N' && str[i] <= 'Z')
str[i] -= 13;
else if(str[i] >= 'a' && str[i] <= 'm')
str[i] += 13;
else if(str[i] >= 'n' && str[i] <= 'z')
str[i] -= 13;
}
}
int main() {
int T;
cin >> T;
cin.get();
for(int step = 0;step < T;step++){
string str;
getline(cin,str);
trans(str);
cout << "case #" << step << ":" << endl;
cout << str << endl;
}
return 0;
}

EOJ 2892

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 <cstdio>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct Data{
int doc_size;
string date, time, doc_name;
};
bool cmp_name(const Data &a, const Data &b){
return a.doc_name < b.doc_name;
}
bool cmp_size(const Data &a, const Data &b){
if(a.doc_size == b.doc_size)
return cmp_name(a, b);
return a.doc_size < b.doc_size;
}
bool cmp_time(const Data &a, const Data &b){
if(a.date == b.date){
if(a.time == b.time)
return cmp_name(a, b);
else
return a.time < b.time;
}
else
return a.date < b.date;
}
int main() {
int n;
while(cin >> n, n != 0){
vector<Data> vec;
Data myData;
for(int i = 0;i < n;i++){
cin >> myData.date >> myData.time >> myData.doc_size >> myData.doc_name;
vec.push_back(myData);
}
string string1, string2;
cin >> string1 >> string2;
if(string2 == "/NAME")
sort(vec.begin(), vec.end(), cmp_name);
else if(string2 == "/SIZE")
sort(vec.begin(), vec.end(), cmp_size);
else if(string2 == "/TIME")
sort(vec.begin(), vec.end(), cmp_time);
for(int i = 0;i < n;i++)
printf("%s %s %16d %s\n", vec[i].date.c_str(), vec[i].time.c_str(), vec[i].doc_size, vec[i].doc_name.c_str());
cout << endl;
}
return 0;
}

EOJ 3018

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
#include <cstdio>
#include <cstring>
#include <string>
#include <cctype>
using namespace std;
void my_gets(char* str) {
*str++ = ' ';
while(*str = getchar(), *str != '\n' && *str != EOF){
if(islower(*str))
*str = toupper(*str);
str++;
}
*str++ = ' ';
*str = '\0';
}
int main() {
int T;
scanf("%d", &T);
getchar();
for(int step = 0;step < T;step++){
char a[20], str[2000000];
my_gets(a);
my_gets(str);
char *p = strstr(str, a);
printf("case #%d:\n", step);
if(p)
printf("%d\n", p-str+1);
else
printf("None\n");
}
return 0;
}

EOJ 3038

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
#include <iostream>
using namespace std;
int main() {
int T;
cin >> T;
for(int step = 0;step < T;step++){
int N;
cin >> N;
string str, res;
cin >> str;
while(!str.empty()){
string str2 = string(str.rbegin(), str.rend());
if(str <= str2){
res += str[0];
str = str.substr(1);
}
else{
int pos = str.size();
res += str[pos-1];
str = str.substr(0, pos-1);
}
}
cout << "case #" << step << ":" << endl;
cout << res << endl;
}
}

EOJ 2974

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
#include <iostream>
#include <vector>
#include <sstream>
#include <cctype>
using namespace std;
const string del[] = {"the","a","an","of","for","and"};
void strTolowwer(string& str) {
for(int i = 0;i < str.size();i++)
if(isupper(str[i]))
str[i] = tolower(str[i]);
}
bool solve(const string& str) {
for(int i = 0;i < 6;i++)
if(str == del[i])
return false;
return true;
}
int main() {
int T;
string str;
cin >> T;
cin.get();
for(int step = 0;step < T;step++){
int cnt = 0;
vector<string> vec;
getline(cin, str);
stringstream ss(str);
while(getline(ss, str, ' ')){
strTolowwer(str);
if(solve(str))
cnt++;
}
cout << "case #" << step << ":" << endl;
cout << cnt << endl;
}
return 0;
}