2 条题解

  • 0
    @ 2025-4-12 20:19:54
    aim = input().lower()
    string = ' ' + input().lower() + ' '
    count = 0
    length = len(aim)
    for i in range(len(string)-length):
        if string[i:i + length + 2] == ' ' + aim + ' ':
            count += 1
            if count == 1:
                position = i
    if count == 0:
        print(-1)
    else:
        print(count,position)
    

    确实简单一点点,@玉米大帝

    • 0
      @ 2025-4-5 19:26:15
      #include<bits/stdc++.h>
      using namespace std;
      string dan,ci,s;
      int cur=-1,ans;
      int main(){
          cin>>ci;
          for(int i=0;i<ci.size();i++)ci[i]=tolower(ci[i]);
          getchar();
          getline(cin,s);
          s+=' ';
          for(int i=0;i<s.size();i++)s[i]=tolower(s[i]);
          for(int i=0;i<s.size();i++){
              if(s[i]==' '){
                  if(dan==ci){
                      ans++;
                      if(cur==-1)cur=i-ci.size();
                  }
                  dan="";
              }
              else dan+=s[i];
          }
          if(ans==0) printf("-1");
          else printf("%d %d",ans,cur);
          return 0;
      }
      

      我觉得用Python做会不会更简单一点

      • 1

      【深基6.例7】[NOIP2011 普及组] 统计单词数

      信息

      ID
      701
      时间
      1000ms
      内存
      256MiB
      难度
      9
      标签
      递交数
      11
      已通过
      3
      上传者