1 条题解

  • 0
    @ 2023-10-25 15:17:49

    鉴定是否为一个完整单词,即单词前后都有一个空格。所以构造匹配条件。

    #include <bits/stdc++.h>
    using namespace std;
     
    int main() {
    	string a,b;
    	getline(cin,a); //word
    	getline(cin,b); //article
     
    	for(int i=0; i<a.size()-1; i++) a[i]=tolower(a[i]);
    	for(int i=0; i<b.size()-1; i++) b[i]=tolower(b[i]);
     
    	a=" "+a+" "; //防止识别仅是某一单词一部分的情况
    	b=" "+b+" "; //防止识别仅是某一单词一部分的情况
    	if(b.find(a)==-1)
    		cout<<"-1"<<endl;
    	else {
    		int idx=b.find(a); //返回a在b中的下标
    		int t=idx;
    		int ans=0;
     
    		while(t!=-1) {
    			ans++;
    			t=b.find(a,t+1); //从字符串b的下标t+1开始查找字符串a,返回a在b中的下标
    		}
     
    		cout<<ans<<" "<<idx;
    	}
     
    	return 0;
    }
    

    信息

    ID
    971
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    3
    已通过
    1
    上传者