string prefix and suffix time
limit:1000ms Memory limit:65536k total
submit:81 accepted:22
Description
The prefix of a string refers to any header of the string. For example, the string "ABBC" prefix has "a", "AB", "ABB", "ABBC". Similarly, any trailing end of the string is the suffix of the string, and the suffix "ABBC" has "C", "BC", "BBC", "Abbc". Now give some strings to find the prefix or suffix with the most characters in each string.
input Inputs The first line is an integer n (1<=n<=50), which indicates that there are n strings.
there are n lines below, each line formatted like this: the string str character ch
string Str consists of a lowercase letter, the length is not more than 100000, the character ch only two values ' P ', ' S '.
if CH is ' P ', then find the prefix with the most character type. If CH is ' S ', then find the suffix with the most character type.
output is one row for each string. The
output format is this: string #X: Y
is in the read order of the string, and X increments from 1 to N.
y is a prefix string or suffix string that satisfies the condition
, or one with the smallest length if more than one prefix or suffix satisfies the condition.
Sample Input
3
abbc p
kaaaaaaaaaa p
nlogn S
sample Output
String #1: ABBC
String #2: ka
String #3: Logn
Source
Cxyue
/*-----------------------------------------------------------------------------------Approximate idea: Example: Scenario: When selecting for P: 1. String
Set a pointer to each tail, compare the values pointed to by the two pointers until the two pointers have the same address 2. If the head pointer refers to a character that is equal to the tail pointer, the tail pointer moves forward one bit, the head pointer resets to the first bit, and repeats 1 3. If the character of the head pointer is not equal to the tail pointer, the head pointer moves forward one bit, Results: Accept, Time 15MS .... It's too weak ...
However, I still did not think of a faster way to. -----------------------------------------------------------------------------------* * #include <stdio.h> #
include<string.h> void Find (char *szstr, int n, char choice);
Char szinputstr[100000];
int main (void) {int i = 1;
int t = 0;
char chchoice = 0;
scanf ("%d", &t);
while (GetChar ()! = ' \ n ') {continue;
} while (t--> 0) {scanf ("%s%c", Szinputstr,&chchoice);
Find (Szinputstr,i++,chchoice);
} return 0;
} void Find (char *szstr, int n, char choice) {char *pszbeg = SZSTR;
Char *pszend = szstr + strlen (szstr)-1;
Char szoutputstr[1000];
char *pendpos = pszend;
char *pbegpos = Pszbeg; int ilen = strlen (SzSTR);
if (' P ' = = choice) {while (Pszbeg! = pszend) {if (*pszbeg = = *pszend) {pszend--;
Pendpos = Pszend;
Pszbeg = Szstr;
} else {pszbeg++;
}} Ilen = Pendpos-szstr + 1;
strncpy (Szoutputstr, Szstr, Ilen);
Szoutputstr[ilen] = ' + ';
} else if (' S ' = = choice) {while (Pszbeg! = pszend) {if (*pszbeg = = *pszend) {pszbeg++;
Pbegpos = Pszbeg;
Pszend = Szstr + iLen-1;
} else {pszend--;
}} pszend = Szstr + iLen-1;
Ilen = Pszend-pbegpos + 1;
strncpy (Szoutputstr, Pbegpos, Ilen);
Szoutputstr[ilen] = ' + ';
} printf ("String #%d:%s\n", N,SZOUTPUTSTR);
}