20分AC on #2 and #4

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

玄关
by ygwlan @ 2023-09-29 19:33:30


@[ygwlan](/user/747917) 可以直接整行读入字符串,然后再find,如果没有找到(也就是返回结果为 `string::npos`)就直接输出-1,否则就接着往下找,找到没有为止,答案从1开始。 ```cpp #include <iostream> using namespace std; string word, s; void f(string &s) { int len = s.size(); for (int i = 0; i < len; i++) { if (s[i] >= 'A' && s[i] <= 'Z') { s[i] += 32; } } } int main() { cin >> word; cin.get(); getline(cin, s); word = ' ' + word + ' '; s = ' ' + s + ' '; f(word), f(s); int pos = int(s.find(word)); if (pos == -1) { cout << -1; } else { int p = pos, cnt = 0; while (p != -1) { cnt++; p = s.find(word, p + 1); } cout << cnt << ' ' << pos; } return 0; } ```
by heyx0201 @ 2023-09-29 19:54:50


@[ygwlan](/user/747917) 求关注qwq
by heyx0201 @ 2023-09-29 19:55:13


@[heyx0201](/user/768951) 蟹蟹
by ygwlan @ 2023-09-29 20:01:01


|