5. Tragic text

Problem description:
You have a broken keyboard. All the keys on the keyboard can work normally, but sometimes the Home key or End key is automatically
pressed. You didn't know the keyboard had this problem, but you focused on typing English words without even looking at the monitor. When you
look at the monitor, what is displayed in front of you is a tragic text. Your task is to count how many words there are in this text.
The input contains multiple sets of data. Each group of data occupies one line and contains no more than 20,000 English letters, spaces, characters "[" or
"]" (the data with up to 20,000 characters will be displayed on one line, don’t worry about line breaks). The character "[" represents the Home key (position the cursor to the beginning of a line), and "]" represents the End key (position the cursor to the end of a line). The end of input symbol is the end of file (EOF). The input text does not exceed 50kB.

For each group of data, output a number, that is, there are several words in this line of tragedy text (a continuous string separated by spaces is one word).
For example, input:
This is a [Beiju] text to
get the tragedy text:
BeijuThis is a text
Therefore, there are 4 words in total.
And input:
This[ ]is[ ]a [Beiju] text to
get the tragedy text:
Beiju Thisisa text
Therefore, there are 3 words in total.

This program is implemented with a singly linked list with a leading node. Please write the count function in it to realize the function of counting the number of words. Please don't try to use array implementation, it will cause the program to run out of time.

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include<time.h>
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;
}
int count(struct node *head)
{
    
    
//请在此输入你的代码,统计单词数目并返回
	

//函数形参为链表头指针,注意,其中第一个结点是头结点
}
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);
    return 0;
}

Input instructions:
multiple groups of text can be input, each group contains one line, including words composed of English letters, spaces, [,]

Output description:
output an integer, which is the number of words

Input example:
This is a [Beiju] text
This[ ]is[ ]a [Beiju] text

Output example:
4
3

int count(struct node *head) {
    
    
	int cnt = 0,flag = 0;	//flag标记一个单词
	struct node *p = head;
	while(p) {
    
    
		if(flag == 0 && p->data != ' ') {
    
    
			cnt++;
			flag = 1;	//一个单词开始了
		}
		if(flag == 1 && p->data == ' ') {
    
    
			flag == 0;	//一个单词结束了
		}
		p = p->next;
	}
	return cnt;
}

Guess you like

Origin blog.csdn.net/qq_37924213/article/details/108481992