为什么把s[i]!='\0改成i<strlen(s)就会tle呢
查看原帖
为什么把s[i]!='\0改成i<strlen(s)就会tle呢
890929
yiban202241040105楼主2023/10/20 17:16
#include<bits/stdc++.h>
using namespace std;
const int N = 1e8 + 1;
#define read(a) {char c;while((c=getchar())>47) a=a*10+(c^48);}
int n, m;
char t[12];
char s[N];
int ans = 0;
int first = 0;
int judge(int x)
{
	int j = 0;
	int i;
	for ( i = x;tolower(s[i])==t[j]&&isalpha(s[i])&&isalpha(t[j]); )
	{
		i++;
		j++;
	}
	if (!isalpha(s[i])&&j==strlen(t))
	{
		if (ans == 0)
			first = x;
		ans++;
	}
	return i;
}
int main() {
	cin.getline(t,12);
	cin.getline(s, N);
	for (int i = 0; i < strlen(t); i++)
	{
		t[i] = tolower(t[i]);
    }
	for (int i = 0; s[i]!='\0'; i++)
	{
		if ((i==0|| !isalpha(s[i-1])) && tolower(s[i]) == t[0])
		{
			i=judge(i);
		}
	}
	if (ans != 0)
		cout << ans << " " << first << endl;
	else
		cout << -1 << endl;
	return 0;
}
2023/10/20 17:16
加载中...