Submission #94340

# Submission time Handle Problem Language Result Execution time Memory
94340 2019-01-17T18:41:13 Z zeyad49 Savez (COCI15_savez) Java 11
120 / 120
550 ms 51504 KB
import java.io.*;
import java.util.*;

class savez{

	
	static int n;
	
	static int MOD=(int)1e9+9,p=31,maxN=(int)2e6+5;;
	
	public static void main(String[] args) throws IOException {
		Scanner sc=new Scanner();
		PrintWriter out=new PrintWriter(System.out);
		
		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 pow=1;
			long last=0;
			for(int j=0;j<len;j++) {
				last=last+(s[j]-'A'+1)*1l*pow;
				last%=MOD;
				hash[j]=(int)last;
				pow=pow*p%MOD;
			}
			
			int dp=1;
			pow=1;
			for(int prefix=len;prefix>0;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%MOD))
					dp=Math.max(dp, 1+map.getOrDefault(preHash, 0));
				pow=pow*p%MOD;
			}
			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 70 ms 9652 KB Output is correct
2 Correct 71 ms 9664 KB Output is correct
3 Correct 74 ms 9832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 9868 KB Output is correct
2 Correct 79 ms 9884 KB Output is correct
3 Correct 120 ms 12736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 312 ms 39080 KB Output is correct
2 Correct 307 ms 38548 KB Output is correct
3 Correct 330 ms 38908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 11964 KB Output is correct
2 Correct 313 ms 28216 KB Output is correct
3 Correct 362 ms 32308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 353 ms 33012 KB Output is correct
2 Correct 419 ms 35432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 381 ms 35324 KB Output is correct
2 Correct 388 ms 35408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 440 ms 39456 KB Output is correct
2 Correct 396 ms 38176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 375 ms 41440 KB Output is correct
2 Correct 495 ms 42968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 427 ms 50552 KB Output is correct
2 Correct 434 ms 49736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 461 ms 50020 KB Output is correct
2 Correct 550 ms 50552 KB Output is correct
3 Correct 501 ms 51504 KB Output is correct