Submission #94340

#TimeUsernameProblemLanguageResultExecution timeMemory
94340zeyad49Savez (COCI15_savez)Java
120 / 120
550 ms51504 KiB
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 (stderr)

Note: savez.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...