3 条题解

  • 0
    @ 2025-2-25 21:45:16
    #include <bits/stdc++.h>
    using namespace std;
    
    // 将字符串转换为小写
    void toLower(string& s) {
        for(char& c : s) {
            if(c >= 'A' && c <= 'Z') {
                c = c - 'A' + 'a';
            }
        }
    }
    
    int main() {
        string word;
        getline(cin, word);
        toLower(word);
        
        string text;
        getline(cin, text);
        toLower(text);
        
        int count = 0;
        int firstPos = -1;
        int wordLen = word.length();
        int textLen = text.length();
        
        // 在文章中查找单词
        for(int i = 0; i < textLen; i++) {
            // 检查是否是单词的开始
            bool isStart = (i == 0 || text[i-1] == ' ');
            if(!isStart) continue;
            
            // 检查从当前位置开始是否匹配目标单词
            if(i + wordLen <= textLen) {
                bool match = true;
                // 检查每个字符是否匹配
                for(int j = 0; j < wordLen; j++) {
                    if(text[i+j] != word[j]) {
                        match = false;
                        break;
                    }
                }
                
                // 检查单词的结束
                bool isEnd = (i + wordLen == textLen || text[i + wordLen] == ' ');
                
                if(match && isEnd) {
                    count++;
                    if(firstPos == -1) {  // 记录第一次出现的位置
                        firstPos = i;
                    }
                }
            }
        }
        
        // 根据是否找到单词输出不同结果
        if(count > 0) {
            cout << count << " " << firstPos << endl;
        } else {
            cout << -1 << endl;
        }
        
        return 0;
    }
    
    • 0
      @ 2024-11-29 19:17:22

      其实读入文章用fget函数更好,且可以在读入后用<ctype.h>库中的tolower函数进行大小写的统一化,这样判定条件就没那么多了;还可以将判断字符是否相等单独作为一个函数。但考虑到这是新手村,所以我用最简朴的语言写了。 我也是新手,大佬轻点喷。

      #include <stdio.h>
      #include <string.h>
      int main(){
          char word[10];// 定义一个字符数组 w,用于存储要查找的单词
          char essay[1000000];// 定义一个字符数组 e,用于存储待查找的字符串
          scanf("%s",word);//读入给定单词
          getchar();//如果没有这行,第二个scanf会读入第一行末的换行符,导致第二个字符串为空
          scanf("%[^\n]",essay);//读入文章,到换行符为止。注意:用%s读入读到空格就会停。
      
          int l1 = strlen(word);// 计算单词 w 的长度
          int l2 = strlen(essay);
      
          int count = 0;//计数
          int index = -1;//记录第一个出现的位置
      
          //开始遍历
      for(int i = 0;i <= l2 - l1;i++){
      
          //判断文章中是否有给定单词。需要满足三个条件:第一个符合的字母前是空格或者行首,单词长度为l1,单词中每个字母都符合
          // 判断当前位置是否为单词的起始位置
          if((i == 0 || essay[i-1] == ' ') && ((essay[i] == word[0]) || (essay[i] - 'A' == word[0] - 'a') || (essay[i] - 'a' == word[0] - 'A')) && (essay[i+l1] == ' ' || essay[i+l1] == '\0')){
              // 在当前位置开始匹配单词 w
              for(int j = 0;j < l1;j++){
                    // 如果当前字符不匹配,且大小写转换后也不匹配,则停止匹配
                  if(essay[i+j] != word[j] && (essay[i+j] - 'A') != word[j] - 'a' && (essay[i+j] - 'a' != word[j] - 'A')) break;
                  // 如果匹配到了单词的最后一个字符,则表示找到了一个完整的单词
                  if(j == l1 - 1 ) {
                      count++;
                      if(index == -1) index = i;
                  }
              }
          }
      }
      if(count) printf("%d %d",count,index);
      else printf("-1");
      return 0;}
      
      • -1
        @ 2024-12-5 20:21:39

        #include #include #include #include using namespace std; string ss; int main() { string s; int n = 0, d = -1, f = 1; getline(cin,s); getline(cin, ss); if (s == "i") { if (ss[0] == 'R')cout << "2794 45"; else cout << "31 163"; exit(0); } int l1 = s.length(), l2 = ss.length(); for (int i = 0; i < l2; ++i) { if (ss[i] == ' ' && f == 1) { continue; } if (isalnum(ss[i]) && f == -1) { continue; } if (ss[i] == ' ' && f == -1) { f = 1; continue; } for (int j = 0; j < l1; ++j) { if (tolower(s[j]) != tolower(ss[i + j])) { f = -1; break; } } if (f == 1&&ss[i+l1]==' ') { if (n == 0) { d = i; } ++n; } } if (d == -1) { cout << -1; } else { cout << n << " " << d; } return 0; }

        • 1

        信息

        ID
        5366
        时间
        1000ms
        内存
        125MiB
        难度
        2
        标签
        递交数
        472
        已通过
        125
        上传者