Blue Bridge Cup · 2011 Popularity Group Statistics of Words

**

2011-2 count the number of words

**
Title:

General text editors have a word search function, which can quickly locate the position of a specific word in the article, and some can also count the number of times a specific word appears in the article.

Now, please program to achieve this function. The specific requirements are: Given a word, please output the number of times it appears in a given article and the position of its first occurrence. Note: When matching words, it is case-insensitive, but it requires an exact match, that is, the given word must be exactly the same as an independent word in the article without being case-sensitive (see example 1), if the given word Only part of a word in the article is not considered a match (see example 2).


enter


The first line is a string, which contains only letters, indicating a given word;

The second line is a string, which may only contain letters and spaces, indicating a given article.


Output


There is only one line. If a given word is found in the article, it will output two integers. The two integers are separated by a space. They are the number of times the word appears in the article and the position of the first occurrence (that is, the first occurrence of the word in the article). When it appears once, the position of the first letter of the word in the article, the position starts from 0); if the word does not appear in the article, an integer -1 is directly output.
Input:
To
to be or not to be is a question
Sample Output 1
Output:
2 0

Example 2:
Input:
to
Did the Ottoman Empire lose its power at that time
Output:
-1

[Description of input and output sample 1]

The output indicates that the given word To appears twice in the article, and the position of the first occurrence is 0.

[Description of input and output sample 2]

Indicates that the given word to does not appear in the article, and the integer -1 is output.

【data range】

1≤word length≤10.

1≤article length≤1,000,000.

Select words directly in the article and compare them when they are the same length as the given word.
Analysis of thoughts
When I saw this question, I crashed, because I didn’t understand the input and output of strings, so I learned various input and output temporarily (tell me to listen carefully in class), including some understanding of it. Practical function, this question needs to compare each word to find the same number of words.

  1. First define an array variable of type char.
  2. Consider case, convert all to lowercase or uppercase, I converted to lowercase here
  3. Use strlen() function to find the length of the input string
  4. According to the length of the string, the loop is traversed, and the same words are found by comparing them one by one.
  5. Output the corresponding value

Code analysis

	scanf("%s",&str1);
	getchar();
	gets(str2);

Note here that scanf cannot directly enter a string with spaces.
A string with spaces needs to be entered with gets(). Since it can be read indefinitely, it may cause overflow.

for(int g=0;g<i;g++){
    
    
	if(str1[g]>='A'&&str1[g]<='Z'){
    
    
		str1[g]=str1[g]+32;
	}
}
for(int z=0;z<t;z++){
    
    
	if(str2[z]>='A'&&str2[z]<='Z'){
    
    
		str2[z]=str2[z]+32;
	}
}

This part of the code is to unify the capitalization, and I unified it to lowercase here. It is easy to understand this string of codes by using the ASCLL code value.
Next is the core key code of this question

for(int i=0;i<=t;i++){
    
    //循环次数为字符串长度
		if(str2[i]>='a'&&str2[i]<='z'){
    
    
		//第一个if判断是否为字母,将对应字母存入数组str3中
			str3[f]=str2[i];
			f++;
		}
		if(str2[i]==' '){
    
    
	//第二个if判断str[i]为空格时先令str3='\0';
			str3[f]='\0';//这行代码很重要,我最开始就是一直卡在这里,这行代码可以保证单词的完整性。不然下面进行比较的时候会出错的。
			if(strcmp(str1,str3)==0){
    
    
	//第三个if是进行判断两个字符串是否相等,用到函数strcmp(),strcmp()返回值为0就表示两个字符串相等。
				count++;//统计出现次数
				if(b==0){
    
    
		//第四个if确定最初出现的位置用当前的i值减去这个str1字符串的长度就是这个单词的第一个字母的数组下标。
					w=i-strlen(str1);
					b=1;
				} 		
			}
			f=0;//初始化f的值,保证str3数组每一次循环都只存入一个单词。
		}
	}
if(count==0){
    
    //count==0则说明没有相同的单词,输出-1
		printf("-1");
	}
	else{
    
    
		printf("%d %d",count,w);
	}

The following is the complete code

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
    
    

	char str1[11],str2[1000001],str3[11]="";
	int j,f=0,t,count=0,w,b=0;
	scanf("%s",&str1);
	getchar();
	gets(str2);
	int i=strlen(str1);
	t=strlen(str2); 
	for(int g=0;g<i;g++){
    
    
		if(str1[g]>='A'&&str1[g]<='Z'){
    
    
			str1[g]=str1[g]+32;
		}
	}
	for(int z=0;z<t;z++){
    
    
		if(str2[z]>='A'&&str2[z]<='Z'){
    
    
			str2[z]=str2[z]+32;
		}
	}
	for(int i=0;i<=t;i++){
    
    
		if(str2[i]>='a'&&str2[i]<='z'){
    
    
			str3[f]=str2[i];
			f++;
		}
		if(str2[i]==' '){
    
    
			str3[f]='\0';
			if(strcmp(str1,str3)==0){
    
    
				count++;
				if(b==0){
    
    
					w=i-strlen(str1);
					b=1;
				} 						
			}
			f=0;
		}
	}
	if(count==0){
    
    
		printf("-1");
	}
	else{
    
    
		printf("%d %d",count,w);
	}
	return 0;
}

Frozen three feet is not a day’s cold, everything needs hard work,
my algorithm is novice, learn from each other’s strengths, make progress slowly, come on, everyone

Guess you like

Origin blog.csdn.net/qq_52044923/article/details/110008150