洛谷刷题5:那一天,我又想起了被字符串支配的恐惧(洛谷P1308)

题目描述

一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数。

现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给定的文章中出现的次数和第一次出现的位置。注意:匹配单词时,不区分大小写,但要求完全匹配,即给定单词必须与文章中的某一独立单词在不区分大小写的情况下完全相同(参见样例1 ),如果给定单词仅是文章中某一单词的一部分则不算匹配(参见样例2 )。

输入格式

22行。

11行为一个字符串,其中只含字母,表示给定单词;

22行为一个字符串,其中只可能包含字母和空格,表示给定的文章。

输出格式

一行,如果在文章中找到给定单词则输出两个整数,两个整数之间用一个空格隔开,分别是单词在文章中出现的次数和第一次出现的位置(即在文章中第一次出现时,单词首字母在文章中的位置,位置从00 开始);如果单词在文章中没有出现,则直接输出一个整数-11。

输入输出样例

输入 #1
To
to be or not to be is a question
输出 #1
2 0

输入 #2
to
Did the Ottoman Empire lose its power at that time
输出 #2
-1
自己的代码:
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<math.h>
#include<string>
char a[1000001],  c[11];
using namespace std;
int main()
{
    cin.getline(c, 11);
    cin.getline(a, 1000001);
    int len = strlen(c);
    for (int i = 0; i < strlen(c); i++)
        if (c[i] >= 'A' && c[i] <= 'Z')
            c[i] = c[i] + 'a' - 'A';
    for (int i = 0; i < strlen(a); i++)
        if (a[i] >= 'A' && a[i] <= 'Z')
            a[i] = a[i] + 'a' - 'A';
    int count = 0, position = 1000;
    for (int i = 0; a[i] != '\0'; i++)
    {
        if (i == 0 && a[i] != ' ')
        {
            int flag = 1, offset = 0;
            for (int j = i; a[j] != ' '&&j<i+len; j++)
            {
                if (a[j] != c[offset++])
                    flag = 0;
            }
            if (flag)
            {
                count++;
                if (position > i)position = i;
            }
        }
        if (a[i] == ' ' && a[i + 1] != ' ')
        {
            int flag = 1, offset = 0;
            for (int j = i+1; a[j] != ' '&&j<i+len+1; j++)
            {
                if (a[j] != c[offset++])
                {
                    flag = 0;
                    break;
                }
            }
            if (flag)
            {
                count++;
                if (position > i)position = i+1;
            }
        }
    }
    if (count)cout << count << " " << position;
    else cout << -1;
    return 0;

}

喜获

实在没办法了,进了讨论版,在大佬们的回复中,找到了这个宝藏代码,五体投地。。。

#include<iostream>
#include<string>
using namespace std;
int main() {
    string a, b;
    getline(cin, a); getline(cin, b);
    for(int i = 0; i < a.size(); ++i) a[i] = tolower(a[i]);
    for(int i = 0; i < b.size(); ++i) b[i] = tolower(b[i]);//统一大小写
    a = ' ' + a + ' ', b = ' ' + b + ' ';
    int p = -1, num = 0;
    while((p = b.find(a, p + 1)) != -1) ++num;
    if(num) cout << num << ' ' << b.find(a);
    else cout << -1;
    return 0;
}

//这里,用到了string中的find函数,其具体用法为:

string a,b;

a.find(b,n)

指在字符串a中查找b,且是从第n位开始,n可以省略,默认为从第0位开始查找

具体参见https://blog.csdn.net/qq_33866063/article/details/88285757?ops_request_misc=%25257B%252522request%25255Fid%252522%25253A%252522161149477816780265490865%252522%25252C%252522scm%252522%25253A%25252220140713.130102334.pc%25255Fall.%252522%25257D&request_id=161149477816780265490865&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-7-88285757.first_rank_v2_pc_rank_v29&utm_term=find%E5%87%BD%E6%95%B0c++

对于自己那又臃肿又可怜的代码,现在还是没有什么头绪,只能等以后再回头来看了

posted @ 2021-01-24 21:32  _翩若惊鸿  阅读(121)  评论(0)    收藏  举报