全部RE,但样例对
查看原帖
全部RE,但样例对
1103858
H3licopter楼主2023/11/10 22:58

求大佬指点

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<limits.h>
#include<cmath>
#include<string.h>
#define ll long long
#define ld long double
#define us unsigned int
using namespace std;

char word[100];
const int lenofarticle = 1e7 + 10;
char passage[lenofarticle] = { ' ' };
int cnt;
int wordcnt = -1; //记录已有单词下标
int firstpos = 10000000; //第一次出现的位置

void charin(char* s, int i)
{
	while ((s[i] = getchar()) != '\n' && s[i] != ' ')
	{
		if ('A' <= s[i] && s[i] <= 'Z')
		{
			s[i] = s[i] - ('A' - 'a');
		}
		i++;
	}
}

void jud()
{
	char* p;
	p = strstr(passage, word);
	if (p != NULL)
	{
		if (*(p - 1) == ' ' && (*(p + strlen(word)) == ' ' || *(p + strlen(word)) == '\n')) //单词左右如果不是字母,那么就是和题意的单词
		{
			cnt++;
			firstpos = min(firstpos, wordcnt);
		}
	}
}

inline void init(int len)
{
	memset(passage, 0, sizeof(char) * len);
	passage[0] = ' ';
}

int main()
{
	charin(word, 0);
	word[strlen(word) - 1] = '\0';

	while (true)
	{
		charin(passage, 1); //文章以逐词读入
		if (passage[1] != ' ')
		{
			wordcnt++;
		}
		jud();
		if (passage[strlen(passage) - 1] == '\n')
		{
			break;
		}
		init(strlen(passage) + 1);
	}



	if (cnt != 0)
	{
		cout << cnt << ' ' << firstpos;
	}
	else
	{
		cout << -1;
	}
}
2023/11/10 22:58
加载中...