首页 > 其他 > 详细

洛谷P1308 统计单词数

时间:2019-10-06 19:44:12      阅读:69      评论:0      收藏:0      [点我收藏+]

原题链接:https://www.luogu.org/problem/P1308

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
string sent,word;
int lens,lenw,t,pos; 
int main()
{
    getline(cin,word);  
    getline(cin,sent);   //字符串含有空格输入,不可以用cin,否则to me只能读到to 
    lenw=word.size();   
    lens=sent.size();
    for(int i=0;i<lenw;i++)
     word[i]=toupper(word[i]);  //toupper转成大写   tolower转化小写      
    for(int i=0;i<lens;i++)
     sent[i]=toupper(sent[i]);
   t=0;
    for(int i=0;i<=lens-lenw;i++)
    {
        int j;
        for(j=0;j<lenw;++j)
        {
            if(sent[j+i]!=word[j]) break;
            if(i>0&&sent[i-1]!= ) break;    //不是单词开头就结束 
            }
            if(j==lenw&&(sent[j+i]== ||j+i==lens)) //单词长度匹配,后面是空格或者句末 
            {
                t++;
                if(t==1) pos=i;
            }
    }
    if(t==0)cout<<-1;
    else cout<<t<<" "<<pos;
    return 0;
}

 

洛谷P1308 统计单词数

原文:https://www.cnblogs.com/QingyuYYYYY/p/11628108.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!