# 31 条题解

• @ 2021-08-29 17:11:21
``````#include <bits/stdc++.h>
using namespace std;

int main()
{
string a,b;
cin>>a;
getchar();
transform(a.begin(),a.end(),a.begin(),::tolower);
getline(cin,b);
b.insert(0," ");
a=a+' ';
a.insert(a.begin(),' ');
transform(b.begin(),b.end(),b.begin(),::tolower);
if(b.find(a)==-1)
{
cout<<"-1";
}
else
{
int sum=0;int n=0;
while(b.find(a,n)!=-1)
{
sum++;
n=b.find(a,n)+1;

}
cout<<sum<<" ";
cout<<b.find(a);
}
return 0;
}
``````
• @ 2018-11-29 22:36:21

思路&要点

由于是单匹配，而且是单词，便读入一个单词匹配一次
注：计算机单词宽泛的定义是不含空格，制表符，换行符的一段最长字符序列

全部将匹配字符利用tolower函数转换为小写
注：原型int tolower(int c)，只可单个转换，C标准库没有针对字符串的转换(VC有内置在<string.h>库中的strlwr

长度统计：我已“空格序+单词”最为分割的段，在getline_my()中统计文章长度，在main中确定单词出现位置

#include <iostream>
#include <string>
using namespace std;
void convert(string &p);
int main(void)
{
string key;
string data;
int flag = 0;
int demo = -1;
getline(cin,key);
getline(cin,data);
convert(data);
convert(key);
int i = 0; int j = 0;
while(data[i] != '\0')
{
if(flag == 0)
{
demo = i;
}
while(data[i] == key[j])
{
if(key[j+1] == '\0' && (data[i+1] == ' ' || data[i+1] == '\0'))
{
flag++;
break;
}
else
{
j++;
i++;
}
}
j = 0;
while(data[i++] != ' ' && data[i] != '\0')
{
}
}
if(flag != 0)
cout << flag << " " << demo;
else
cout << -1;
return 0;
}
void convert(string &p)
{
int i = 0;
while(p[i] != '\0')
{
if(p[i] >= 'A' && p[i] <= 'Z' )
p[i] += 32;
i++;
}
}

• @ 2020-04-12 18:56:19
``````#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

char transform(char a)  //大写转变为小写
{
if(a >= 'A' && a <= 'Z')
a += 32;
return a;
}

int main()
{
int ans = 0, p;
string s0, str;
cin >> s0;
getchar();
getline(cin, str);
for (int i = 0; i < str.length(); i++)
if(i == 0 || str[i - 1] == ' ')
{
int j, k;
for (j = 0, k = i; j < s0.length(); j++, k++)
if (transform(s0[j]) != transform(str[k]))
break;
if(j == s0.length() && (k == str.length() || str[k] == ' '))
{
ans++;
if (ans == 1)
p = i;
}
}

if(ans)
cout << ans << " " << p <<endl;
else
cout << -1 << endl;

return 0;
}

``````
• @ 2019-08-18 22:53:30

服了，看错题两次，是全部字母不区分大小写，不仅仅是第一个。没有匹配的直接输出-1不是0 -1。
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

bool IsWord(char *word, char *article, int i,int len);

int main()
{
char word[10],article[1000001];
int num=0,place,len=0;
scanf("%s",word);
int i=0,j;
while(getchar()!='\n');
while ((article[i]=getchar())!=EOF)
i++;
i=0;
while (isalpha(word[len]))
len++;
while (article[i]!=EOF)
{
if (article[i]==word[0]||article[i]==word[0]+'A'-'a'||article[i]==word[0]+'a'-'A')
{
if (IsWord(word,article,i,len))
{
num++;
if (num==1)
place=i;
}
}
i++;
}
if (num==0)
printf("-1");
else{
printf("%d %d",num,place);
}
return 0;
}

bool IsWord (char *word, char *article, int i,int len)
{
bool ans=true;
int j=1;
if (i==0)
{
for (;j<len;j++)
{
if (word[j]!=article[i+j]&&word[j]!=article[i+j]+'a'-'A'&&word[j]!=article[i+j]+'A'-'a')
{
ans=false;
break;
}
}
if (article[i+len]!=' '&&article[i+len]!=EOF&&article[i+len]!='\n')
{ans=false;}
}else{
if(article[i-1]!=' ')
ans=false;
for (;j<len&&ans;j++)
{
if (word[j]!=article[i+j]&&word[j]!=article[i+j]+'a'-'A'&&word[j]!=article[i+j]+'A'-'a')
ans=false;
}
if (article[i+len]!=' '&&article[i+len]!=EOF&&article[i+len]!='\n')
ans=false;
}
return ans;
}

• @ 2018-02-06 10:06:46
``````#include<iostream>
#include<cstring>
#include<cstdio>
int count=0,cur=0,l=0;
void chg(std::string &a) {
for(int i=0; a[i]!='\0'; i++) {
if(a[i]>='A'&&a[i]<='Z') {
a[i]+=32;
}
}
}
int main() {
char temp;
bool b=false;
std::string str1,str2;
std::cin>>str1;
while(std::cin.peek()!='\n') {
std::cin.get();
}
std::cin.get();
chg(str1);
while(std::cin.peek()!='\n') {
temp=std::cin.peek();
if(temp==' ') {
std::cin.get();
l++;
continue;
}
b=false;
std::cin>>str2;
l+=str2.length();
chg(str2);
if(str1==str2) {
count++;
b=true;
}
if(b&&count==1) {
cur=l-str2.length();
}
}
if(count==0) {
printf("%d", count-1);
} else {
printf("%d %d", count, cur);
}
return 0;
}
``````
• @ 2016-08-25 14:47:15
``````var s1,s2:ansistring;i,cnt,posa:longint;
begin
cnt:=0;
for i:=1 to length(s1) do begin  //convert
if s1[i] in ['A'..'Z'] then s1[i]:=chr(ord(s1[i])+32);
end;
for i:=1 to length(s2) do begin
if s2[i] in ['A'..'Z'] then s2[i]:=chr(ord(s2[i])+32);
end;
s1:=' '+s1+' ';
s2:=' '+s2+' ';
posa:=-1;
while pos(s1,s2)>0 do
begin
inc(cnt);
if posa=-1 then posa:=pos(s1,s2)-1;
delete(s2,1,pos(s1,s2));
end;
if posa=-1 then writeln(posa) else writeln(cnt,' ',posa);
end.
``````
• @ 2014-12-02 19:34:18

#include<iostream>
using namespace std;
main()
{
int a=0,f;
string s,w;
string::size_type i;
cin>>w;
w=" "+w+" ";
for(i=0;i<w.size();i++)
w[i]=toupper(w[i]);
getline(cin,s);
getline(cin,s);
s=" "+s+" ";
for(i=0;i<s.size();i++)
s[i]=toupper(s[i]);
for(i=s.find(w);i<s.size();i=s.find(w,i+w.size()-1),a++)
if(!a)
f=i;
if(a)
cout<<a<<' '<<f;
else
cout<<-1;
}

• @ 2014-03-28 13:55:22

今天发现用pos也不会超时代码如下
var
s1,s2:ansistring;
a,i,ans,k:longint;
first:boolean;
begin
s1:=' '+upcase(s1)+' ';
s2:=' '+upcase(s2)+' ';
first:=true;
while pos(s1,s2)>0 do
begin
a:=pos(s1,s2);
delete(s2,1,a+length(s1)-2);
if first then begin k:=a-1; first:=false; end;
inc(ans);
end;
if ans>0 then writeln(ans,' ',k)
else writeln('-1');
end.

• @ 2014-03-26 14:03:18

一、数据量大用annistring
二、别用pos查找要超时，用逐个比较统计的方法
三、注意输出，第一个位子输出为0，以此类推

• @ 2014-01-26 21:27:21

测试数据 #0: Accepted, time = 0 ms, mem = 840 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 840 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 844 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 1064 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 1064 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 1064 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 1068 KiB, score = 10
测试数据 #7: Accepted, time = 78 ms, mem = 3124 KiB, score = 10
测试数据 #8: Accepted, time = 46 ms, mem = 3128 KiB, score = 10
测试数据 #9: Accepted, time = 78 ms, mem = 3124 KiB, score = 10
Accepted, time = 202 ms, mem = 3128 KiB, score = 100
程序:
var
s1,s2,tmp:ansistring;
i,cs,wz,len:longint;
flag:boolean;
begin
s2:=' '+s2+' ';
wz:=-1;
for i:=1 to length(s1) do
if (s1[i]>='A')and(s1[i]<='Z')
then s1[i]:=chr(ord(s1[i])+32);
for i:=1 to length(s2) do
if (s2[i]>='A')and(s2[i]<='Z')
then s2[i]:=chr(ord(s2[i])+32);
len:=length(s1);
for i:=length(s2)-length(s1)+1 downto 1 do
begin
tmp:=copy(s2,i,len);
if (tmp=s1)and((s2[i-1]<'a')or(s2[i-1]>'z'))and((s2[i+len]<'a')or(s2[i+len]>'z'))
then begin
inc(cs);
wz:=i-2;
flag:=true
end
end;
if flag
then write(cs,' ');
writeln(wz)
end.

• @ 2013-11-09 09:17:02

var s,a:ansistring;
i,j,n,m,k,ans:longint;
c:char;

begin
ans:=0;
s:=lowercase(s);
j:=0;
while not eoln do
begin
a:='';
m:=1;
while (c<>' ')and(not eoln) do
begin
a:=a+c;
m:=m+1;
end;
a:=lowercase(a)+' ';
if a=s then
begin
inc(ans);
if ans=1 then k:=j;
end;
j:=m+j;
end;
if ans<>0 then
writeln(ans,' ',k)
else writeln(-1);
end.
一坑水题 花了我一晚上 题目数据第一行最后有空格 测试中没有

• @ 2013-11-09 09:16:58

var ans,dect,i,j,m:longint;
s,w:string;
c:char;

begin
dect:=maxlongint;
i:=0;
ans:=0;

while not(eoln) do
begin
s:='';
if c<>' ' then j:=i;
while (c<>' ')and(not(eoln)) do begin s:=s+c; inc(i); read(c); end;
inc(i);
if upcase(s)=upcase(w) then
begin
if j<dect then dect:=j;
inc(ans);
end;
end;
if ans=0 then write(-1) else
write(ans,' ',dect);
end.

• @ 2013-08-14 13:59:39

#include <iostream>
#include <string.h>
using namespace std;
const int INF = 1000005 ;
char a[15],b[INF],s[INF];
int main()
{
// freopen("stat.in","r",stdin);
// freopen("stat.out","w",stdout);
int i,j,k=0,n1,n2,p;
bool flag;
cin.getline(a,10); strlwr(a) ; n1= strlen(a);
cin.getline(b,1000000);strlwr(b); n2= strlen(b);
for (i=0 ; i< n2 ;i++){
if ((b[i-1]!=' ' && i!=0)|| (b[i+n1]!=' ' && i+n1!=n2)) continue;
flag=1;
for (j=0;j < n1;j++)
if (b[i+j]!=a[j]) {flag=0;break;}
if (flag) {
k++;
if (k==1) p=i;

}
}
if (k>0) cout<<k<<" "<<p<<endl; else cout<<"-1"<<endl;
// system("pause");
return 0;
}

• @ 2013-08-13 19:07:49

=。= 原来是lowercase
我说写的downcase怎么不对
一开始我竟然没看到要输出第一次出现的位置还WA了一次= =||
Var word,s:ansistring;
n,i,j,ans,tmp,F:longint;
flag,first:boolean;
Begin
first:=false;
For i:=1 to length(word) do
If (word[i]>='a') and (word[i]<='z') then
word[i]:=upcase(word[i]);
word:=word+' ';
For i:=1 to length(s) do
If (s[i]>='a') and (s[i]<='z') and (s[i]<>' ') then
s[i]:=upcase(s[i]);
s:=s+' ';
For i:=1 to length(s) do
If (s[i]=word[1]) and ((s[i-1]=' ') or (i=1)) then
Begin
flag:=false;
for j:=1 to length(word) do
If word[j]<>s[i+j-1] then begin flag:=true; break; end;
If not flag then
Begin
If not first then
Begin
first:=true;
f:=i-1;
End;
inc(ans);
End;
End;
If ans=0 then begin writeln(-1); halt; end;
Writeln(ans,' ',f);
End.

• @ 2012-11-25 13:50:09

灰常简单的样子啊...

由于测评机WAITING，交了好几次，不过过了

├ 测试数据 01：答案正确... (16ms, 476KB)

├ 测试数据 02：答案正确... (31ms, 476KB)

├ 测试数据 03：答案正确... (0ms, 476KB)

├ 测试数据 04：答案正确... (31ms, 736KB)

├ 测试数据 05：答案正确... (78ms, 736KB)

├ 测试数据 06：答案正确... (31ms, 736KB)

├ 测试数据 07：答案正确... (31ms, 736KB)

├ 测试数据 08：答案正确... (94ms, 1768KB)

├ 测试数据 09：答案正确... (109ms, 1768KB)

├ 测试数据 10：答案正确... (78ms, 1764KB)

程序：

program P1761;

var

b,c:string;

a:ansistring;

i,j,k,l,m:longint;

df:boolean;

procedure pd;

begin

df:=false;

if (i=1)and(a=' ') then df:=true;

if (a=' ')and(a=' ') then df:=true;

if (i=length(a))and(a=' ') then df:=true;

end;

begin

l:=length(b);

for i:=1 to l do

b[i]:=lowercase(b[i]);

for i:=1 to length(a)-l+1 do

begin

pd;

if df then begin

c:=copy(a,i,l);

for j:=1 to l do

c[j]:=lowercase(c[j]);

if b=c then begin

if k=0 then m:=i-1;

k:=k+1;

end;  end;  end;

if k=0 then writeln('-1') else

write(k,' ',m);

end.

• @ 2012-11-14 21:31:14

├ 测试数据 01：答案正确... (47ms, 484KB)

├ 测试数据 02：答案正确... (31ms, 484KB)

├ 测试数据 03：答案正确... (0ms, 484KB)

├ 测试数据 04：答案正确... (0ms, 744KB)

├ 测试数据 05：答案正确... (0ms, 744KB)

├ 测试数据 06：答案正确... (0ms, 744KB)

├ 测试数据 07：答案正确... (47ms, 744KB)

├ 测试数据 08：答案正确... (172ms, 1776KB)

├ 测试数据 09：答案正确... (203ms, 1776KB)

├ 测试数据 10：答案正确... (156ms, 1776KB)

var

i,l,ans,ans1:longint;

s,s1,ch:ansistring;

function pd(a:string):boolean;

var

i:longint;

p:boolean;

begin

p:=true;

if length(s)length(a) then p:=false

else begin

i:=length(s);

while(i>0)do

begin

if(a[i]='A') then a[i]:=chr(ord(a[i])-ord('A')+ord('a'));

if a[i]s[i] then begin p:=false;   break; end;

dec(i);

end;

end;

pd:=p ;

end;

begin

l:=length(s);

for i:=1 to l do

if (s[i]='A') then

s[i]:=chr(ord(s[i])-ord('A')+ord('a'));

l:=length(s1);

ch:='';

ans:=0;

ans1:=0;

for i:=1 to l do begin

if s1[i]' ' then ch:=ch+s1[i]

else begin

if pd(ch) then begin

inc(ans);

if ans=1 then begin

ans1:=i-length(s)-1;

end;

end;

ch:='';

end;

end;

if ans=0 then writeln(-1)

else writeln(ans,' ',ans1);

end.

分享一下吧！

• @ 2012-11-03 13:18:41

├ 测试数据 01：答案正确... (0ms, 520KB)

├ 测试数据 02：答案正确... (0ms, 520KB)

├ 测试数据 03：答案正确... (0ms, 520KB)

├ 测试数据 04：答案正确... (0ms, 744KB)

├ 测试数据 05：答案正确... (0ms, 744KB)

├ 测试数据 06：答案正确... (0ms, 744KB)

├ 测试数据 07：答案正确... (0ms, 744KB)

├ 测试数据 08：答案正确... (0ms, 2804KB)

├ 测试数据 09：答案正确... (0ms, 2804KB)

├ 测试数据 10：答案正确... (0ms, 2804KB)

---|---|---|---|---|---|---|---|-

Accepted / 100 / 0ms / 2804KB

注意：1、用ansistring

2、pos会超时，读字符串后截取单词进行长度比较，长度相同进行单词比较

3、输出的应该是 首次出现位置-1

4、忽略大小写的方法是upcase或lowercase

注:upcase(s:string或ansistring),系统函数，把s的所有小写字母换成大写

lowercase(s:string或ansistring),系统函数，把s的所有大写字母换成小写

===================庆祝AC第90题===================

• @ 2018-11-07 23:00:15

#include<bits/stdc++.h>
using namespace std;
int main()
{
string x;
cin>>x;
int len=x.length();
int i=0;
if(x[i]=='-')
{
cout<<'-';
i++;
}
while(1)
{
if(x[len-1]=='0')
len--;
else
break;
}
for(;len>i;len--)
cout<<x[len-1];
return 0;

}

• @ 2018-07-06 16:21:43

Hash解法
注意，测试样例时请自行去除行末的空格。

``````#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
typedef unsigned long long ULL;//把unsigned long long简写
const int b=31;
const int MAXN=1000050;//数组大小
string t1;//输入的单词
string t2;//输入的文章
char s1[MAXN];//输入的单词
char s2[MAXN];//输入的文章
long long power[MAXN];//用来存放b&n
long long sum[MAXN];//用来存放主串hash值
int main() {
int i,pos=-1;//第一次出现的位置，如果没有就是-1
power[0]=1;//b^0=1
for (int i=1;i<MAXN;++i)//计算MAXN次
power[i]=power[i-1]*b;//计算b*n
getline(cin,t1);//用getline读入一整行
getline(cin,t2);//用getline读入一整行
int s1len=t1.length(),s2len=t2.length();//存放长度
for (int i=0;i<s1len;i++) s1[i+1]=tolower(t1[i]);//放进s1数组并转换大小写，注意这里往后移了一位
for (int i=0;i<s2len;i++) s2[i+1]=tolower(t2[i]);//放进s2数组并转换大小写，注意这里往后移了一位
sum[0]=0;//初始化
for (int i=1;i<=s2len;++i)
sum[i]=sum[i-1]*b+s2[i];//计算主串hash值
ULL s=0;//用来存放子串hash值
for (int i=1;i<=s1len;++i)
s=s*b+s1[i];//计算子串hash值
int tot=0;//计数器
for (int i=0;i<=s2len-s1len;++i)
if(s==sum[i+s1len]-sum[i]*power[s1len]&&(i==0||s2[i]==' ')&&(i+s1len==s2len||s2[i+s1len+1]==' '))//如果hash值匹配，并且这个词前后是空格（或者在文章开头/结尾）
{
if (pos==-1) pos=i;//如果是第一个，保存位置
++tot;//总词数加1
}
if (pos==-1) cout<<-1;//没有找到，输出-1
else cout<<tot<<' '<<pos<<endl;//输出次数、位置
return 0;//结束程序
}
``````
• @ 2017-08-22 02:50:38

so water

``````#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
int t=0,tt=0;
char s[1000001],ss[11];
gets(ss);
gets(s);
for(int i=0;i<=strlen(s)-strlen(ss);++i)
{
int j;
for (j=0;j<=strlen(ss)-1;++j)
{
if (toupper(s[j+i])!=toupper(ss[j]))
break;
if (i>0&&s[i-1]!=' ')
break;
}
if(j==strlen(ss)&&(s[j+i]==' '||j+i==strlen(s)))
{
t++;
if(t==1)
tt=i;
}
}
if(t==0)
printf("-1");
else
printf("%d %d\n",t,tt);
return 0;
}
``````

ID
1761

7

2768

539

19%

12