File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+
3+ import java.io.* ;
4+ import java.util.* ;
5+
6+ public class Main {
7+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
9+ static StringTokenizer st;
10+ static int [] p;
11+
12+ public static void main (String [] args ) throws IOException {
13+ char [] str = br. readLine(). toCharArray();
14+ char [] pattern = br. readLine(). toCharArray();
15+ p = new int [pattern. length];
16+
17+ for (int i = 1 ,j = 0 ; i < pattern. length; i++ ) {
18+ while (j> 0 && pattern[i] != pattern[j]){
19+ j = p[j- 1 ];
20+ }
21+ if (pattern[i] == pattern[j]){
22+ p[i] = ++ j;
23+ }
24+ }
25+ char [] res = new char [str. length];
26+ int [] len = new int [str. length];
27+ int ptr = 0 ;
28+ for (int i = 0 ,j; i < str. length; i++ ) {
29+ res[ptr] = str[i];
30+ j = (ptr == 0 ) ? 0 : len[ptr- 1 ];
31+ while (j> 0 && str[i] != pattern[j]){
32+ j = p[j- 1 ];
33+ }
34+ if (str[i] == pattern[j]) {
35+ j++ ;
36+ }
37+ len[ptr] = j;
38+ ptr++ ;
39+
40+ if (j == pattern. length){
41+ ptr -= j;
42+ }
43+ }
44+ if (ptr == 0 ){
45+ bw. write(" FRULA" );
46+ }else {
47+ bw. write(new String (res,0 ,ptr));
48+ }
49+ bw. close();
50+ }
51+ }
52+ ```
You can’t perform that action at this time.
0 commit comments