子字符串寻址

Description
给定两字符串S和s(字符串仅由a~z组成),扫描s是否在S中出现.如果出现,给出它出现的位置和S中与串s相邻的字符,例如:S = "abcdefg", s = "cd", 则输出为 3 b e ; 否则输出0.
Input
第一行为测试样例数N. 对于每个样例,包含字符串S和s,它们之间用 换行符'\n'隔开。0 < N < 100 , 0 < strlen(s) <= strlen(S) <= 1000000.
Output
对于每一对S与s, 输出一相应的结果.
Sample Input
3

hheef
hh
ccdde
de
fedd
ze
Sample Output
1 e

4 d
0

没想到测试数据那么大的,还得用getchar缓存数据!

#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
char a[1000001],s[1000001];
void find(char a[],char s[])
{
int n,m,i,j,k;
m=strlen(s);
n=strlen(a);
k=0;
for(i=0;i<=(n-m);i++)
{
j=0;
while(j<m&&s[j]==a[j+i])
{
j++;
if(j==m)
{
k=i+1;
break;
}
}

}
if(k==0)cout<<0<<endl;
else if(k==1) cout<<k<<" "<<a[k+m-1]<<endl;
else if(k==n-m+1) cout<<k<<" "<<a[k-2]<<endl;
else cout<<k<<" "<<a[k-2]<<" "<<a[k+m-1]<<endl;

}
int main()
{
int t;
cin>>t;
getchar();//用这个起缓存数据作用,有效避免超时!
while(t--)
{
scanf("%s",a);
scanf("%s",s);
find(a,s);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值