python求助,只ac了2个

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

```python s0=input().lower() str=list(map(str,input().lower().split())) t=str.count(s0) if t==0: print(-1) else: i=str.index(s0) print(f"{t} ",end="") print(i) ```
by xiuwenjuan_22333 @ 2023-03-18 12:48:54


``` #include<bits/stdc++.h> char a[11],c[11]; char b[1000001]; char bb[1000003]; using namespace std; int main(){ int la,lb,cnt,pos; cin.getline(a,11); cin.getline(b,10000001); la=strlen(a); lb=strlen(b); for(int i=0;i<la;i++) a[i]=tolower(a[i]); for(int i=0;i<lb;i++) b[i]=tolower(b[i]); bb[0]=' '; strcat(bb,b); strcat(bb," "); cnt=0; for(int i=1;i<=lb-la+1;i++){ strncpy(c,bb+i,la); c[la]='\0'; if(strcmp(a,c)==0 && bb[i-1]==' '&&bb[i+la]==' '){ cnt++; if (cnt==1) pos=i-1; } } if (cnt==0) cout<<"-1"; else cout<<cnt<<" "<<pos; return 0; } ```
by zjhzs666 @ 2023-03-18 13:14:38


```python #求给定的单词在给定的文章中出现的次数 #及单词首字母在文章中出现的位置 s = input().lower()#给定的单词,其中只含字母,转为小写 word = input().lower() #给定的文章,可能包含字母和空格,转为小写 words = word.split() #给定的文章按空格分割成单词列表,这样单词都是不含空格的 #在前面加空格是为了匹配文章中第一个单词就是给定单词的情况; #在后面加空格是为了匹配文章中最后一个单词是给定单词的情况; word=' '+word + ' ' if s not in words: print(-1) else: #s前后加空格是因为s在文章中作为一个单词出现,它的前后字符必定是空格 print(words.count(s), word.find(" "+s+' ')) ```
by tang_123458 @ 2024-03-26 11:05:10


|