悲剧文本 C++

悲剧文本

问题描述 :

你有一个破损的键盘。键盘上的所有键都可以正常工作,但有时Home键或者End键会自动按下。你并不知道键盘存在这一问题,而是专心地输入英文单词,甚至连显示器都没瞧一眼。当你看显示器时,展现在你面前的是一段悲剧的文本。你的任务是计算这段文本有几个单词。

输入包含多组数据。每组数据占一行,包含不超过20000个英文字母、空格、字符 “[” 或者 “]”(这多达20000个字符的数据会显示在一行,不用担心会换行)。其中字符“[”表示Home键(将光标定位到一行的开头),“]”表示End键(将光标定位到一行的结尾)。输入结束标志为文件结束符(EOF)。

输入文本不超过50kB。对于每组数据,输出一个数字,即这一行悲剧文本中共有几个单词(以空格分隔的连续字符串为一个单词)。

比如,输入:
This is a [Beiju] text
得到的悲剧文本是:
BeijuThis is a text
因此,共有4个单词。

而输入:
This[ ]is[ ]a [Beiju] text
得到的悲剧文本是:
Beiju Thisisa text
因此,共有3个单词。

输入说明 :
可输入多组文本,每组包含一行,其中包括英文字母组成的单词、空格、[、]

输出说明 :
输出一个整数,为单词数目

输入范例 :
This is a [Beiju] text
This[ ]is[ ]a [Beiju] text

输出范例 :
4
3

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include<time.h>
#include<iostream>
using namespace std;
struct node
{
    char data;
    struct node *next;
};

struct node* create(char text[])
{
    int i;
    struct node *current;//当前插入位置指针
    struct node *tail;//尾指针
    struct node *head;//头指针
    head = (struct node*)malloc(sizeof(struct node));//头结点
    head->next = NULL;
    current = head;//当前位置指针指向头结点
    tail = head;//尾指针也指向头结点
    for(i=0; i<strlen(text); i++)
    {
        if(text[i] == '[')
        {//当前位置指针回到最前面
            current = head;
        }
        else if (text[i] == ']')
        {//当前位置指针移到最后面
            current = tail;
        }
        else
        {//在当前位置指针后面插入结点
            struct node *p;
            p = (struct node*)malloc(sizeof(struct node));
            p->data = text[i];
            p->next = current->next;
            current->next = p;
            current = p;
            if(current->next == NULL) tail = current; //当前位置在最后面,则需要修改tail指针
        }
    }
    return head;
}
void  displayLink(node *head)
{
	struct node *p=head->next;
	cout<<"head-->";
	while(p!= NULL)
	{
		cout<<p->data<<"-->";
		p=p->next;
	}
	cout<<"tail\n";
}
int count(struct node *head)//因为已经把[]里的内容自动提前了,所以只需要根据单词的字母前后是否有空格即可判断是否是一个单词
{
//请在此输入你的代码,统计单词数目并返回
    struct node *p=head->next;
    int num=0,isornot=0;
    while(p!=NULL)
    {
        if(isornot==0&&p->data!=' ')
        {
            num=num+1;
            isornot=1;
        }
        if(isornot==1&&p->data==' ')
        {
            isornot=0;
        }
        p=p->next;
    }
    return num;
    //函数形参为链表头指针,注意,其中第一个结点是头结点
}

int main()
{
        //freopen("input2.txt","r",stdin);
        //freopen("output2.txt", "w", stdout);
        char text[100005];
        struct node *head,*p;
        int num;
        while(gets(text)!=NULL)
        {
            head=create(text);
            //for(p=head->next; p!=NULL; p=p->next)
            //    printf("%c",p->data);
            //printf("\n");
            num=count(head);
            printf("%d\n",num);
        }
        //printf("%.2f\n", (double)clock()/CLOCKS_PER_SEC);
        //displayLink(head);这边自己验证了一下创建出来的列表输出来的时候是什么内容,发现已经把带[]的提到前面去了
        return 0;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值