Submission #94339

# Submission time Handle Problem Language Result Execution time Memory
94339 2019-01-17T18:37:22 Z zeyad49 Savez (COCI15_savez) Java 11
108 / 120
552 ms 66560 KB
import java.io.*;
import java.util.*;

class savez{

	
	static int n;
	
	static int MOD=(int)1e9+9,p=31,pow[],maxN=(int)2e6+5;;
	
	public static void main(String[] args) throws IOException {
		Scanner sc=new Scanner();
		PrintWriter out=new PrintWriter(System.out);
		pow=new int [maxN];
		pow[0]=1;
		for(int i=1;i<maxN;i++) {
			long x=pow[i-1]*1l*p%MOD;
			pow[i]=(int)x;
		}
		n=sc.nextInt();
		int ans=1;
		HashMap<Integer,Integer> map=new HashMap();
		for(int i=0;i<n;i++) {
			char []s=sc.next().toCharArray();
			int len=s.length;
			int []hash=new int [len];
			
			long last=0;
			for(int j=0;j<len;j++) {
				last=last+(s[j]-'A'+1)*1l*pow[j];
				last%=MOD;
				hash[j]=(int)last;
			}
			
			int dp=1;
			for(int prefix=1;prefix<=len;prefix++) {
				int preHash=hash[prefix-1],sufHash=hash[len-1]-(prefix==len?0:hash[len-1-prefix]);
				if(sufHash<0)
					sufHash+=MOD;
			
				
				if(sufHash==(preHash*1l*pow[len-prefix]%MOD))
					dp=Math.max(dp, 1+map.getOrDefault(preHash, 0));
			}
			map.put(hash[len-1], dp);
			ans=Math.max(ans, dp);
		}
		out.println(ans);
		
		
		
		out.close();

	}
	
	static class Scanner
	{
		BufferedReader br;
		StringTokenizer st;
		Scanner(){
			br=new BufferedReader(new InputStreamReader(System.in));
		}
		Scanner(String fileName) throws FileNotFoundException{
			br=new BufferedReader(new FileReader(fileName));
		}
		String next() throws IOException {
			while(st==null || !st.hasMoreTokens())
				st=new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		String nextLine() throws IOException {
			return br.readLine();
		}
		int nextInt() throws IOException{
			return Integer.parseInt(next());
		}
		long nextLong()  throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}
		double nextDouble() throws NumberFormatException, IOException {
			return Double.parseDouble(next());
		}
	}
}

Compilation message

Note: savez.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
# Verdict Execution time Memory Grader output
1 Correct 133 ms 17784 KB Output is correct
2 Correct 132 ms 17804 KB Output is correct
3 Correct 138 ms 18204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 140 ms 18360 KB Output is correct
2 Correct 131 ms 17916 KB Output is correct
3 Correct 177 ms 22516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 366 ms 55052 KB Output is correct
2 Correct 341 ms 47288 KB Output is correct
3 Correct 397 ms 56596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 240 ms 20324 KB Output is correct
2 Correct 314 ms 36928 KB Output is correct
3 Correct 373 ms 40420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 332 ms 41084 KB Output is correct
2 Correct 352 ms 43008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 382 ms 43252 KB Output is correct
2 Correct 398 ms 46144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 433 ms 47956 KB Output is correct
2 Correct 403 ms 46052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 404 ms 57916 KB Output is correct
2 Correct 519 ms 60208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 475 ms 59520 KB Output is correct
2 Correct 521 ms 58900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 429 ms 59348 KB Output is correct
2 Correct 552 ms 59208 KB Output is correct
3 Runtime error 470 ms 66560 KB Execution killed with signal 9 (could be triggered by violating memory limits)