Home > Back-end >  Please run results yes to this question, why say it's an array is submitted?
Please run results yes to this question, why say it's an array is submitted?

Time:10-03

# include
# include
using namespace std;

Int next [1000];

Int KMP (string s1, string s2) {
Int I=0, j=0;
While (iIf (j==1 | | s1. The substr (I, 1)==s2. The substr (j, 1)) {
++i;
+ + j;
}
The else j=next [j];
}

If (j>=(int) s2. The length ())
Return I - s2. The length ();
The else return - 1;
}

Void getnext (string s) {
Int j=0, k=1;
Next [0]=1;

While (j<(int) s.l ength ()) {
If (k==1 | | s.s ubstr (j, 1)==s.s ubstr (k, 1)) {
+ + j;
+ + k;
Next [j]=k;
}
The else k=next [k].
}
}

Int main () {
Int t, I, j;
cin> t;
String s [1000] [10].
for(i=0; ifor(j=0; j<2; J++)
cin> S [I] [j];

for(i=0; iFor (j=s [I] [1] the length () - 1; J>=0; J -)
S [I] [1]. Append (s [I] [1]. The substr (j, 1));
}
for(i=0; iGetnext (s [I] [1]).


for(i=0; iIf (KMP (s [0], [I] s [I] [1])==0)
cout<1 & lt; The else cout
return 0;
}

CodePudding user response:

Photo is not clear, can't see requirements
However, getnext (s [I] [1]). If the length of the s [I] [1] greater than 1000 (s are append before [I] [1], the length of the really can't say for certain), the next [j]=k; The next array could cross

CodePudding user response:

reference 1st floor qybao response:
title picture is not clear, can't see requirements
However, getnext (s [I] [1]). If the length of the s [I] [1] greater than 1000 (s are append before [I] [1], the length of the really can't say for certain), the next [j]=k; The next array could cross

Problem description: palindrome is the correct sequence, the reverse is the same string, such as aba, abba, refers to the reverse, s s - 1
Given a number T, said the next group T data,
Each group of data, including S and P (length is less than 1000), separated by a space, please find out first in the S shape such as PP - 1 back text string and output starting subscript, if there is no output - 1,

CodePudding user response:

reference 1st floor qybao response:
title picture is not clear, can't see requirements
However, getnext (s [I] [1]). If the length of the s [I] [1] greater than 1000 (s are append before [I] [1], the length of the really can't say for certain), the next [j]=k; The next array could cross

Topic is less than 1000, I put the array has been set to 1000, but still crossing the line, how can I change?

CodePudding user response:

cin> S [I] [j];//assume here enter the length of 1000

for(i=0; iFor (j=s [I] [1] the length () - 1; J>=0; J -)
S [I] [1]. Append (s [I] [1]. The substr (j, 1));//append here will be more than 1000
So getnext (s [I] [1]). It is possible to cross the next array

CodePudding user response:

reference 4 floor qybao response:
cin> S [I] [j];//assume here enter the length of 1000

for(i=0; iFor (j=s [I] [1] the length () - 1; J>=0; J -)
S [I] [1]. Append (s [I] [1]. The substr (j, 1));//append here will be more than 1000
So getnext (s [I] [1]). The next array could cross

Assuming maximum is 1000, and a reverse a total of 2000, I put the next array size expanded to 3000 or not

CodePudding user response:

Run results of cross-border this contradiction at the same time, int a [100]; A [100]=10; Cout & lt; CodePudding user response:

refer to 6th floor ggglivw response:
run results of cross-border this contradiction at the same time, int a [100]. A [100]=10; Cout & lt;

But I'm not set next array into 10000, according to the topic string length is less than 1000, a character corresponds to a next value, even with reverse after the next number value also can not be more than 10000, don't know where to cross a line

CodePudding user response:

You can compile the code through?

CodePudding user response:

Didn't see your getnext function is what purpose
And also not your KMP
Write a sample for you, yourself reference
 int main (int arg c, const char * argv []) {
Int T;
The scanf (" % d ", & amp; T);
Char S [1001], [1001] P;
Int * result=new int [T];
Int I, j, k=0, t, ls, lp.
For (I=0; iThe scanf (" % s % s ", s, P);
Ls=(int) strlen (S); The length of the//S
Lp=(int), strlen (P); The length of the//P
Result [k]=1;
If (ls & lt; 2 * lp) {//if S length less than 2 times of length of P is the next group (S to include P and P - 1, the length must be greater than or equal to 2 times the length of the P
continue;
}
For (j=0; j<=2 * ls - lp; J++) {//starting from j S position loop through
If (memcmp (S + j, P, lp)==0) {//if S + j to include P
For (t=0; TIf t==(lp) {//t reach the lp shows S + j + lp contains P - 1
Result [k]=j;//save find j position
break;
}
}
}
}
For (I=0; iPrintf (" % d \ n ", the result [I]);
}
The delete [] the result;
return 0;
}
  • Related