Finding a problem today is the last of a written test. I implemented it in C language. The overall feeling is not difficult, to share with you.
To see the topic:
Given a query and a text, all are made up of lowercase letters.
Requires that the length of the longest consecutive alphabetic sequence appearing consecutively in query in the same order be found in text.
For example, query is "Acbac" and text is "Acaccbabb",
Then the "CBA" in text is the longest sequence of letters appearing consecutively in query, so the returned result should be 3 of its length.
In general, we find the number of matching small strings in the string.
The general idea should be: Start with the ' a ' in text, equal to ' a ' in query, and two increments. The ' C ' in text is equal to ' C ' in query, and then two increments. At this point, ' B ' in text is not equal to ' a ' in query. Return to ' a ' in text, ' C ' in query (increased by one). The next step is to start with the ' a ' in text, compared to the second character in query ' C ' ..., and so on. Wait until the ' a ' in text is compared to the characters in query. In the turn ' C ', then ' B ' ..., and so on.
Look at the code:
#include <stdio.h>#include <stdlib.h>#include <string.h>/* Given a query and a text, all are made up of lowercase letters. Requires that the length of the longest consecutive alphabetic sequence appearing consecutively in query in the same order be found in text. For example, if query is "Acbac" and text is "Acaccbabb", then "CBA" in text is the longest sequence of letters that appears consecutively in query, so the return result should be its length 3*///query: "Acbac"//text: "Acaccbabb"intQueryChar*query,Char*text) {intI,j,len,len2,max =0,Count=0; Len = strlen (text); Len2 = strlen (query);if(query = = NULL | | text = NULL | | Len <=0) {return-1; } for(i=0; i<len;i++)//loop for text{intTMP = i;Count=0; for(j=0; j<len2;j++) {if(Text[tmp] = = Query[j])//Same words at the same time move{Count++; tmp++;if(Count> max) max =Count; }Else{tmp = i; J-=Count;//j-count is the first J, and then J + + is where it should start again . Count=0;Continue; } } }returnMax;}intMain () {intmax = Query ("Ccbac","Acdbabac"); printf"%d", Max);return 0;}
Results:
3
After manual inspection, the result is correct. It is worth noting that I only returned the corresponding number in the text, and did not return the matching pile of strings, you can try, how to return the matching string. You can try to add arguments to the function, save the matching string in the text, and return the string. The possibilities of code are endless, and everyone can try.
Written questions (1)