Noip习题
—————————————————————————————————
字符串
统计单词数 Noip2011 普及
整体思路是利用双指针 快慢指针来进行遍历
竞赛中可以使用substr 然后利用字符串的== 进行比较 类似方法如下
但是不能全部改成大写or小写 可以加一个特判
但考虑到练习的效果 于是写了个双指针
// ConsoleApplication63.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <string>
using namespace std;
string a, b;
int le1, le2;
int main()
{
getline(cin, a);
getline(cin, b);
le1 = a.length();
le2 = b.length();
int frise = 0;
int time = 0;
int q = 0;//慢
int p = 0;
while (p < le2 && q<le2)
{
int x = 0;//用来遍历x指针
int head = q;
while (b[p] != ' '&& p < le2)
{
p++;
}
//p到达了空格的位置
while (q<p && x<le1 && tolower(b[q]) == tolower(a[x]))
{
x++;
q++;
}
if (q == p && x == le1)// 确保 q 和x都遍历到了最后
{
if (time == 0)
frise = head;
time++;
}
p++;
q++;
}
if (!time)
cout << -1;
else
cout<< time << " " << frise;
}