src
stringlengths
95
64.6k
complexity
stringclasses
7 values
problem
stringlengths
6
50
from
stringclasses
1 value
import java.util.*; public class a{ public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.next(); int max = 0; for(int i=0; i<str.length(); i++) { for(int j=i+1; j<=str.length(); j++) { String first = str.substring(i,j); for(int k=i+1; k<=str.length()-first.length(); k++) { if(str.substring(k,k+first.length()).equals(first)) max = Math.max(max,first.length()); } } } System.out.println(max); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Scanner; public class Main implements Runnable { /** * @param args */ public static void main(String[] args) { new Thread(new Main()).start(); } public void run() { Locale.setDefault(Locale.US); try { run1(); } catch (IOException e) { throw new RuntimeException(); } } int nextInt(StreamTokenizer st) throws IOException { st.nextToken(); return (int) st.nval; } private List<Integer> kmp(String x, String a) { String s = a + "$" + x; int[] oppa = new int[s.length()]; oppa[0] = 0; int tmp = 0; List<Integer> res = new ArrayList<Integer>(); for (int i = 1; i < s.length(); i++) { while (tmp != 0 && s.charAt(tmp) != s.charAt(i)) { // System.out.println(i + " " + tmp); tmp = oppa[tmp - 1]; } if (s.charAt(tmp) == s.charAt(i)) tmp++; oppa[i] = tmp; if (tmp == a.length()) { res.add(i - a.length() - a.length()); } } return res; } double nextDouble(StreamTokenizer st) throws IOException { st.nextToken(); return st.nval; } String nextLine(StreamTokenizer st) throws IOException { st.nextToken(); return st.sval; } int cnt = 0; int[] col; void unite(int a, int b) { if (cnt % 2 == 0) col[getCol(a)] = getCol(b); else col[getCol(b)] = getCol(a); cnt++; } int getCol(int a) { return a == col[a] ? a : (col[a] = getCol(col[a])); } public void run1() throws IOException { // Scanner sc = new Scanner(new FileReader("input.txt")); Scanner sc = new Scanner(new InputStreamReader(System.in)); // BufferedReader br = new BufferedReader(new // InputStreamReader(System.in)); // PrintWriter pw = new PrintWriter(new FileWriter("output.txt")); String s= sc.next(); int res = 0; int n = s.length(); for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++) { int k = 0; while(j + k < n && s.charAt(i + k) == s.charAt(j + k)) k++; res = Math.max(res, k); } System.out.println(res); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.math.*; import java.util.*; public class Main { public static void main(String[] args) /*throws FileNotFoundException*/ { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA{ static boolean twofind(String s, String t){ int index = s.indexOf(t); int index2 = s.indexOf(t, index + 1); if(index2 != -1)return true; return false; } public void solve(int testNumber, FastScanner scan, FastPrinter out) /*throws FileNotFoundException*/ { //Scanner sscan = new Scanner(new File("input.txt")); //PrintStream oout = new PrintStream(new File("output.txt")); String s = scan.next(); String ans = ""; boolean ok = false; for(int i = s.length(); i >= 0; i--){ for(int j = 0; j <= s.length() - 1; j++){ try{ if(twofind(s, s.substring(j, j + i))){ ans = s.substring(j, j + i); break; } }catch(Exception e){ break; } } if(!ans.equals(""))break; } out.println(ans.length()); //sscan.close(); //oout.close(); } } class FastScanner extends BufferedReader { public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try{ int ret = super.read(); return ret; }catch(Exception e){ throw new InputMismatchException(); } } public String next() { StringBuilder sb = new StringBuilder(); int c = read(); while (isWhiteSpace(c)) { c = read(); } if (c < 0) { return null; } while (c >= 0 && !isWhiteSpace(c)) { sb.appendCodePoint(c); c = read(); } return sb.toString(); } static boolean isWhiteSpace(int c) { return c >= 0 && c <= 32; } public int nextInt() { int c = read(); while (isWhiteSpace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int ret = 0; while (c >= 0 && !isWhiteSpace(c)) { if (c < '0' || c > '9') { throw new NumberFormatException("digit expected " + (char) c + " found"); } ret = ret * 10 + c - '0'; c = read(); } return ret * sgn; } public long nextLong() { return Long.parseLong(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } public BigDecimal nextBigDecimal(){ return new BigDecimal(next()); } public String readLine(){ try{ return super.readLine(); }catch(IOException e){ return null; } } } class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; import java.lang.*; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); String str=sc.next(); sc.close(); int maxm=0; int ind1,ind2; for(int i=0;i<str.length();i++){ for(int j=i+1;j<str.length();j++){ int len=0; ind1=i;ind2=j; while(ind2<str.length() && str.charAt(ind1)==str.charAt(ind2)){ ind1++; ind2++; len++; } maxm=Math.max(maxm,len); } } System.out.println(maxm); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.math.*; import java.util.*; public class StringRepeat { static Scanner in = new Scanner( new BufferedReader( new InputStreamReader( System.in ) ) ); public static void main( String[] args ) { String s = in.next(); int n = s.length(), ans = 0; for( int i = 0; i < n; i++ ) for( int j = i+1; j < n; j++ ) { int l = 0; while( j+l<n && s.charAt(i+l)==s.charAt(j+l) ) l++; ans = Math.max( ans, l ); } System.out.println( ans ); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.*; import static java.lang.Math.*; public class BetaRound23_A implements Runnable { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws IOException { if (ONLINE_JUDGE) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } @Override public void run() { try { long t1 = System.currentTimeMillis(); init(); solve(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = " + (t2 - t1)); } catch (Exception e) { e.printStackTrace(System.err); System.exit(-1); } } public static void main(String[] args) { new Thread(new BetaRound23_A()).start(); } void solve() throws IOException { char[] s = in.readLine().toCharArray(); int n = s.length; for (int ans = n - 1; ans >= 1; ans--) { for (int i = 0; i < n - ans + 1; i++) { for (int j = i + 1; j < n - ans + 1; j++) { int count = 0; for (int k = 0; k < ans; k++) { if (s[i + k] == s[j + k]) count++; else break; } if (count == ans) { out.print(ans); return; } } } } out.print(0); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class P19 { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); Map<Integer, Integer> mapa = new HashMap<Integer, Integer>(); String str = in.next(); int len = str.length(); int maxCurrent = 0; for (int i = 0; i < len; ++i) { for (int j = 1; j <= len; ++j) { if (i + j > len) continue; //System.out.format("Adding from %d to %d -> %s\n",i, i+j,str.substring(i, i + j)); int hashCode = str.substring(i, i + j).hashCode(); Integer current = mapa.get(hashCode); if (current == null) current = 0; current++; mapa.put(hashCode, current); if (current > 1) maxCurrent = Math.max(maxCurrent, j); } } out.println(maxCurrent); out.flush(); out.close(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; public class j { public static void main(String a[])throws IOException { BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); int n=0,i=0,k=2; String s="\0",r="\0"; s=b.readLine(); n=s.length()-1; while(k<=s.length()) { for(i=0;i<k;i++) { r=s.substring(i,i+n); if(s.indexOf(r)!=s.lastIndexOf(r)) { System.out.print(n); System.exit(0); } } k++; n--; } System.out.print("0"); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.*; public class Main { static Scanner in; static PrintWriter out; // static StreamTokenizer in; static int next() throws Exception {in.nextToken(); return (int) in.nval;} public static void main(String[] args) throws Exception { in = new Scanner(System.in); out = new PrintWriter(System.out); // in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); String s = in.next(); int n = s.length(); int max = 0; for (int i = 1; i < n; i++) { String[] subs = new String[n - i + 1]; for (int j = 0; j + i <= n; j++) subs[j] = s.substring(j, j + i); Arrays.sort(subs); boolean flag = false; for (int j = 0; j < n - i; j++) if (subs[j].equals(subs[j + 1])) flag = true; if (flag) max = Math.max(max, i); } out.println(max); out.close(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; public class A23 { public static void main(String[]args){ Scanner sc = new Scanner(System.in); String W = sc.next(); ArrayList<String>Q = new ArrayList<String>(); for (int i = 0; i < W.length(); i++){ String O = ""; for (int k = i; k < W.length(); k++){ O = O + W.charAt(k); Q.add(O); } } Collections.sort(Q); String tmp = Q.get(0); int y = 0; for (int i = 1; i < Q.size(); i++){ if (Q.get(i).equals(tmp)){ if (Q.get(i).length() > y){ y = Q.get(i).length(); } } else { tmp = Q.get(i); } } System.out.println(y); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; import java.io.*; import static java.util.Arrays.*; import static java.lang.Math.*; import java.math.*; public class Main { void run() throws IOException { String s = token(); HashSet <String> h; int n = s.length(); int r = 0; loop: for (int i = 1; i <= n; i++) { h = new HashSet(); for (int j = 0; j < n - i + 1; j++) { String t = s.substring(j, j + i); if (h.contains(t)) { r = i; continue loop; } else { h.add(t); } } } System.out.println(r); } public static void main(String[] args) throws IOException { Locale.setDefault(Locale.US); //final String FILENAME = "dvd"; //in = new BufferedReader(new FileReader(new File(FILENAME + ".in"))); //out = new PrintWriter(new File(FILENAME + ".out")); in = new BufferedReader(new InputStreamReader(System.in)); //in = new Scanner(System.in); out = new PrintWriter(System.out); st = new StringTokenizer(" "); new Main().run(); out.close(); } static BufferedReader in; //static Scanner in; static PrintWriter out; static StringTokenizer st; String token() throws IOException { while (!st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } int nint() throws IOException { return Integer.parseInt(token()); } long nlong() throws IOException { return Long.parseLong(token()); } double ndouble() throws IOException { return Double.parseDouble(token()); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.*; public class Solution { private BufferedReader in; private PrintWriter out; private StringTokenizer st; void solve() throws IOException { String s = next(); HashSet<String> set = new HashSet<String>(); int ans = 0; for (int i = 0; i < s.length(); ++i) { for (int j = i + 1; j <= s.length(); ++j) { String t = s.substring(i, j); if (set.contains(t)) { ans = Math.max(ans, t.length()); } set.add(t); } } out.println(ans); } Solution() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); eat(""); solve(); in.close(); out.close(); } private void eat(String str) { st = new StringTokenizer(str); } String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } eat(line); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } public static void main(String[] args) throws IOException { new Solution(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.Scanner; public class GivenString { public static void main(String[] args) { Scanner input = new Scanner(System.in); String s = input.nextLine(); int max = 0; for(int i = 0; i < s.length(); i++) { for(int j = i + 1; j <= s.length(); j++) { String tmp = s.substring(i, j); int match = 0; for(int k = 0; k + tmp.length() <= s.length(); k++) { if(tmp.equals(s.substring(k, k + tmp.length()))) { match++; } } if(match >= 2) { max = Math.max(max, tmp.length()); } } } System.out.println(max); System.exit(0); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; public class Main { public static void main(String []args)throws Exception { String inp=""; String res=""; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); inp=br.readLine(); for(int i=0;i<inp.length();i++)//counts the length { for(int j=0;j<(inp.length()-i);j++)//start index of string { for(int k=j+1;k<=inp.length()-i;k++) { if(inp.substring(j,j+i).equals(inp.substring(k,k+i))) res =inp.substring(j,j+i); } } } System.out.println(res.length()); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class A23 { static int solve(String s) { for(int i = s.length(); i > 0; --i) { for(int start = 0; start < s.length() - i; ++start) { String str = s.substring(start, start + i); int firstIndex = s.indexOf(str); int lastIndex = s.lastIndexOf(str); if(firstIndex != lastIndex) return i; } } return 0; } public static String[] EX = new String[] { "abcd", "ababa", "zzz", "qwertyuiopasdfghjklzxcvbnmqwepriuwpoep"}; public static int[] EX_A = new int[] { 0, 3, 2, 3}; public static void main(String[] args) throws IOException { if(true) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); System.out.println(solve(s)); } else { for(int i = 0; i < EX.length; ++i) { int result = solve(EX[i]); System.out.println(i + ": " + result + " " + (result == EX_A[i]? "ja" : "NEJ")); } } } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; import java.io.*; public class Longest { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); int max=0; for(int i=0;i<str.length();i++) { for(int x=0;x+i<=str.length();x++) { if(contains(str,str.substring(x,x+i),x)) { //System.out.println(str.substring(x,x+i)); max=Math.max(max, i); } } } System.out.println(max); } public static boolean contains(String whole,String part,int start) { if(whole.indexOf(part, start+1)>=0)return true; return false; } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class A { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = in.readLine(); for (int i = s.length() - 1; i > 0; i--) for (int j = 0; j <= s.length() - i; j++) if (s.substring(0, j + i - 1).contains(s.substring(j, j + i)) || s.substring(j + 1).contains(s.substring(j, j + i))) { System.out.println(i); return; } System.out.println(0); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; public class C23A { public static void main(String args[]){ Scanner sc=new Scanner(System.in); String str=sc.next(); for(int k=str.length()-1;k>=1;k--){ for(int i=0;i<=str.length()-k;i++){ for(int j=i+1;j<=str.length()-k;j++){ if(str.substring(i,i+k).equals(str.substring(j,j+k))){ System.out.println(k); return; } } } } System.out.println(0); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); StringBuilder text = new StringBuilder(in.nextLine()); String substr; String max=""; for(int i=2; i<=text.length(); i++){ for(int j=0; j<i; j++){ substr = text.substring(j, i); if(text.lastIndexOf(substr) != text.indexOf(substr)){ if(substr.length() > max.length()){ max = substr;} } } } System.out.println(max.length()); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.math.*; import java.util.*; import java.util.Map.Entry; public class StringsProb { private void solve() throws IOException { String s = nextToken(); int res = 0; Map<String , Integer> m = new HashMap<String, Integer>(); for (int i = 0; i < s.length(); i++) for (int j = 0; j <= s.length(); j++) { if (i > j) continue; String a = s.substring(i , j); if (a.equals("")) continue; if (m.containsKey(a)) { m.put(a, m.get(a) + 1); } else m.put(a, 1); } for (Entry<String , Integer> e : m.entrySet()) { if (e.getValue() >= 2) res = Math.max(res, e.getKey().length()); } System.out.println(res); } public static void main(String[] args) { new StringsProb().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int[] readIntArray(int size) throws IOException { int[] res = new int[size]; for (int i = 0; i < size; i++) { res[i] = nextInt(); } return res; } long[] readLongArray(int size) throws IOException { long[] res = new long[size]; for (int i = 0; i < size; i++) { res[i] = nextLong(); } return res; } double[] readDoubleArray(int size) throws IOException { double[] res = new double[size]; for (int i = 0; i < size; i++) { res[i] = nextDouble(); } return res; } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } BigInteger nextBigInteger() throws IOException { return new BigInteger(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.*; public class substring { static BufferedReader br; static StringTokenizer st; static PrintWriter out; public static void main(String[] args) throws IOException { InputStream input = System.in; //InputStream input = new FileInputStream("fileIn.in"); OutputStream output = System.out; //OutputStream output = new FileOutputStream("fileOut.out"); br = new BufferedReader(new InputStreamReader(input)); out = new PrintWriter(output); String in = br.readLine(); int len = in.length(); int doub = len; boolean found = false; while (!found) { int count = 0; String[] parts = new String[len - doub + 1]; for (int i = 0; i < len - doub + 1; i++) parts[i] = in.substring(i,i+doub); for (int i = 1; i < len - doub + 1; i++) for (int j = 0; j < i; j++) if (parts[i].equals(parts[j])) count++; if (count >= 1) found = true; doub--; } out.println(doub+1); out.close(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.Scanner; public class Main{ public static void main(String args[]){ Scanner cin = new Scanner(System.in); String str; int i,j,k; int cnt = 0; char [] strArray; str = cin.next(); strArray = str.toCharArray(); for(i = 0; i < strArray.length; i ++) for(j = i + 1; j < strArray.length; j ++) { for(k = 0; (((i + k) < strArray.length && (j + k) < strArray.length) && (strArray[i + k] == strArray[j + k])); k ++) if(k + 1> cnt) cnt = k + 1; } System.out.println(cnt); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; public class A23 { public static void main(String[] args){ Scanner sc = new Scanner(System.in); char[] input = sc.nextLine().toCharArray(); int[][] dyn = new int[input.length][input.length]; int max = 0; for(int a = 0; a < input.length; a++) { for(int b = a + 1; b < input.length; b++) { if(input[a] == input[b]) { int prev = (a == 0) ? 0 : dyn[a-1][b-1]; dyn[a][b] = prev + 1; max = (dyn[a][b] > max) ? dyn[a][b] : max; } } } System.out.println(max); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.Scanner; import java.lang.String; public class A23 { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); int i=0,j=0,n=0,t=0,count=0; n=s.length(); String s1="y",s2="yu6j"; for(t=1;t<n;t++) { for(i=0;i<t;i++) { s1=s.substring(i,i+n-t); for(j=i+1;j<=t;j++) { s2=s.substring(j,j+n-t); if(s1.equalsIgnoreCase(s2)) { count++;break; } if(count==1) break; } if(count==1) break; } if(count==1) break; } if(n==0) { System.out.println("0"); } else { if(count==1) { System.out.println(s1.length()); } else System.out.println("0"); } } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.*; public class A { String line; void run()throws IOException{ BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); line = bf.readLine(); int i, j, len = line.length(), max=0; for(i=0; i<len; i++){ for(j=i; j<len; j++){ if(line.indexOf(line.substring(i,j+1), i+1)>0){ if(j-i+1>max) max = j-i+1; } } } System.out.println(max); } public static void main(String[] args)throws IOException { new A().run(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.BufferedReader; import java.io.PrintWriter; import java.io.InputStreamReader; import java.io.IOException; import java.util.*; public class CF23A implements Runnable{ public static void main(String args[]){ new CF23A().run(); } @Override public void run(){ try{ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); tok = null; solve(); in.close(); out.close(); } catch(IOException e){ e.printStackTrace(); System.exit(0); } } int nextInt()throws IOException{ return Integer.parseInt(nextToken()); } double nextDouble()throws IOException{ return Double.parseDouble(nextToken()); } long nextLong() throws IOException{ return Long.parseLong(nextToken()); } String nextToken()throws IOException{ while(tok == null || !tok.hasMoreTokens()){ tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } BufferedReader in; PrintWriter out; StringTokenizer tok; ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// private void solve()throws IOException{ String s = nextToken(); int l = s.length(); int ans = 0; for(int i = 0; i < l - 1; i++){ for(int j = i + 1; j < l; j++){ String now = s.substring(i, j); if(s.substring(i + 1).indexOf(now) >= 0){ ans = Math.max(ans, j - i); } } } out.println(ans); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; public class Solution { public static void main(String[] args) { new Solution().calc(); } void calc() { Scanner cin = new Scanner(System.in); String s = cin.next(); int ret = 0; for (int i = 0; i < s.length(); i++) { for (int j = i + 1; j < s.length(); j++) { for (int k = 0; j + k < s.length(); k++) { if (s.charAt(i + k) != s.charAt(j + k)) break; ret = Math.max(k + 1, ret); } } } System.out.println(ret); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.Scanner; public class A023 { public static void main(String[] args) { System.out.println(f()); } static int f() { Scanner in = new Scanner(System.in); String line = in.next(); for (int length = line.length(); length > 0; length--) { for (int start = 0; start + length <= line.length(); start++) { if(line.indexOf(line.substring(start,start+length),start+1)>=0) { return length; } } } return 0; } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; import static java.lang.Math.*; import static java.math.BigInteger.*; import static java.lang.Character.*; import static java.util.Arrays.*; import static java.util.Collections.*; public class A { public void run() { Scanner sc = new Scanner(System.in); String s = sc.next(); int n = s.length(); String[] ss = new String[n]; for (int i = 0; i < n; i++) ss[i] = s.substring(i); sort(ss); int res = 0; for (int i = 1; i < n; i++) res = max(res, count(ss[i - 1], ss[i])); System.out.println(res); } int count(String s, String t) { int ret = 0; for (int i = 0; i < min(s.length(), t.length()); i++) if (s.charAt(i) != t.charAt(i)) return ret; else ret++; return ret; } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } public static void main(String... args) { new A().run(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.Scanner; public class A_YoureGivenAString { //1:11 public static void main(String[] args) { Scanner s = new Scanner(System.in); String str = s.nextLine(); for (int l = str.length()-1; l >= 1; l--) { for (int i = 0; i < str.length()-l+1; i++) { String subs = str.substring(i, i+l); if(str.lastIndexOf(subs) != i){ System.out.println(l); return; } } } System.out.println(0); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; import java.io.*; public class Longest { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); int max=0; for(int i=0;i<str.length();i++) { for(int x=0;x+i<=str.length();x++) { if(contains(str,str.substring(x,x+i),x)) { //System.out.println(str.substring(x,x+i)); max=Math.max(max, i); } } } System.out.println(max); } public static boolean contains(String whole,String part,int start) { if(whole.indexOf(part, start+1)>=0)return true; return false; } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.PrintWriter; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); String s = in.nextLine(); int ans = 0; outer: for (int i = s.length() - 1; i >= 1; i--) for (int j = 0; j < s.length() - i; j++) { String sub = s.substring(j, j + i); String str = s.substring(j + 1); if (str.contains(sub)) { ans = i; break outer; } } out.print(ans); out.close(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line = in.readLine(); int n = line.length(); int maxlenght = 0; for (int i = 0; i < n; i++) { int j = line.indexOf(line.charAt(i), i + 1); while (j != -1) { int k = i; int l = j; while (k < n && l < n && line.charAt(k) == line.charAt(l)) { k++; l++; } if (k - i > maxlenght) { maxlenght = k - i; } j = line.indexOf(line.charAt(i), j + 1); } } System.out.println(maxlenght); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.HashSet; import java.util.InputMismatchException; import java.util.Set; /** * @author Egor Kulikov ([email protected]) * Created on 14.03.2010 */ public class TaskA implements Runnable { private InputReader in; private PrintWriter out; public static void main(String[] args) { new Thread(new TaskA()).start(); // new Template().run(); } public TaskA() { // String id = getClass().getName().toLowerCase(); // try { // System.setIn(new FileInputStream(id + ".in")); // System.setOut(new PrintStream(new FileOutputStream(id + ".out"))); // System.setIn(new FileInputStream("input.txt")); // System.setOut(new PrintStream(new FileOutputStream("output.txt"))); // } catch (FileNotFoundException e) { // throw new RuntimeException(); // } in = new InputReader(System.in); out = new PrintWriter(System.out); } public void run() { // int numTests = in.readInt(); // for (int testNumber = 0; testNumber < numTests; testNumber++) { // out.print("Case " + (testNumber + 1) + ": "); // } String s = in.readString(); Set<String> m = new HashSet<String>(); int ans = 0; for (int i = 0; i < s.length(); i++) { for (int j = i + 1; j <= s.length(); j++) { if (m.contains(s.substring(i, j))) ans = Math.max(ans, j - i); else m.add(s.substring(i, j)); } } out.print(ans); out.close(); } private static class InputReader { private InputStream stream; private byte[] buf = new byte[1000]; private int curChar, numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuffer buf = new StringBuffer(); int c = read(); while (c != '\n' && c != -1) { buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) return readLine(); else return readLine0(); } public BigInteger readBigInteger() { try { return new BigInteger(readString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.Scanner; public class A { public static void main(String[] args) { new A().run(); } private void run() { Scanner sc = new Scanner(System.in); String s = sc.next(); sc.close(); int res = 0; for (int i = 0; i < s.length(); i++) for (int j = i + 1; j <= s.length(); j++) { String sub = s.substring(i, j); int c = count(s, sub); if (c >= 2) res = Math.max(res, sub.length()); } System.out.println(res); } private int count(String s, String sub) { int res = 0; while (s.length() > 0) { if (s.startsWith(sub)) res++; s = s.substring(1); } return res; } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.math.BigInteger; public class Main { BufferedReader in; PrintWriter out; public static void main(String[] args) throws IOException { new Main().run(); } public void run() throws IOException { //in=new BufferedReader(new FileReader("input.txt")); //out=new PrintWriter(new FileWriter("output.txt")); in=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(new OutputStreamWriter(System.out)); solve(); out.flush(); } public void solve() throws IOException { String now=in.readLine(); int l=now.length(); int answ=0; for(int i=0;i!=l;i++) for(int j=i+1;j<l;j++) { String a=now.substring(i, j); for(int k=i+1;k<l-j+i+1;k++) if(a.compareTo(now.substring(k, k+j-i))==0) answ=Math.max(answ, a.length()); } out.print(answ); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args)throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); int max = 0; for (int i = 0; i < s.length(); i++) { for (int j = i+1; j < s.length(); j++) { if(s.substring(i+1).contains(s.substring(i,j))) max = Math.max(max, j-i); }} System.out.println(max); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class A { public static void main(String[] args) throws Exception{ String str = new Scanner(System.in).next(); Set<String> set = new HashSet<String>(); String max = ""; for(int l = 1; l < str.length(); ++l){ for(int i = 0; i < str.length()-l; ++i){ String substr = str.substring(i, i+l); if(!set.contains(substr) && str.indexOf(substr) != str.lastIndexOf(substr)){ set.add(substr); if(substr.length() > max.length()){ max = substr; } } } } System.out.println(max.length()); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; public class A { public static void main(String args[]) { Scanner scan = new Scanner(System.in); String str = scan.next(); for(int i=str.length();i >= 1;i--) { for(int j=0;j + i <= str.length();j++) { String sub = str.substring(j, j+i); int index = str.indexOf(sub, j+1); if(index > -1) { System.out.println(i); return; } } } System.out.println(0); } }
cubic
23_A. You're Given a String...
CODEFORCES
import static java.lang.Math.*; import static java.util.Arrays.*; import java.io.*; import java.util.*; public class Main { static boolean LOCAL = false;//System.getSecurityManager() == null; Scanner sc = new Scanner(System.in); void run() { char[] cs = sc.nextLine().toCharArray(); int res = 0; for (int s1 = 0; s1 < cs.length; s1++) { for (int s2 = s1 + 1; s2 < cs.length; s2++) { int len = 0; while (s2 + len < cs.length && cs[s1 + len] == cs[s2 + len]) { len++; } res = max(res, len); } } System.out.println(res); } class Scanner { BufferedReader br; StringTokenizer st; Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); eat(""); } void eat(String s) { st = new StringTokenizer(s); } String nextLine() { try { return br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } boolean hasNext() { while (!st.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } String next() { hasNext(); return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } void debug(Object...os) { System.err.println(deepToString(os)); } public static void main(String[] args) { if (LOCAL) { try { System.setIn(new FileInputStream("in.txt")); } catch (Throwable e) { LOCAL = false; } } if (!LOCAL) { try { Locale.setDefault(Locale.US); System.setOut(new PrintStream(new BufferedOutputStream(System.out))); } catch (Throwable e) { } } new Main().run(); System.out.flush(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.*; public class a23 { public static void main(String args[])throws IOException { InputStreamReader read=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(read); String s,subs; // System.out.println("Enter"); s=in.readLine(); int i,j,k,l=0,a=1,sl=0; for(i=0;i<s.length();i++) { a=1; for(j=i;j<s.length();j++) { subs=s.substring(i,i+a); for(k=i;k<(s.length()-a+1);k++) { if(subs.compareTo(s.substring(k,k+a))==0) l++; if(l==2) { if(a>sl) sl=a; l=0; break; } } l=0; a++; } } System.out.println(sl); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.StringTokenizer; public class A { public static void main(String[] args) throws IOException { new A().solve(); } BufferedReader br; StringTokenizer st = new StringTokenizer(""); private void solve() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); String s = nextToken(); int res = 0; for (int i = 0; i < s.length(); i++) { for (int j = i + 1; j < s.length(); j++) { int k = 0; while (k < s.length() - j && s.charAt(i + k) == s.charAt(j + k)) { k++; } res = Math.max(res, k); } } PrintWriter pw = new PrintWriter(System.out); pw.println(res); pw.close(); } String nextToken() throws IOException { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; import java.io.*; import java.math.*; public class Main { public static Scanner scan = new Scanner(System.in); public static boolean bg = true; public static void main(String[] args) throws Exception { String k1 = scan.next(); HashSet<String> met = new HashSet(); String ans = ""; for (int i=1;i<=k1.length()-1;i++){ for (int j=0;j+i<=k1.length();j++){ String cur = k1.substring(j, j+i); if (!met.contains(cur)){ met.add(cur); } else { if (cur.length()>ans.length())ans=cur; } } } System.out.println(ans.length()); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; public class A23 { static StreamTokenizer in; static PrintWriter out; static int nextInt() throws IOException { in.nextToken(); return (int)in.nval; } static String nextString() throws IOException { in.nextToken(); return in.sval; } public static void main(String[] args) throws IOException { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); String s = nextString(); for (int i = s.length(); i > 0; i--) { for (int j = 0; j+i-1 < s.length(); j++) for (int k = j+1; k+i-1 < s.length(); k++) if (s.substring(j, j+i).equals(s.substring(k, k+i))) { out.println(i); out.flush(); return; } } out.println("0"); out.flush(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.HashMap; import java.util.Scanner; public class Solution { public static void main(String [] args){ Scanner in = new Scanner(System.in); String ins = in.nextLine(); HashMap <String,Integer> sub = new HashMap<String,Integer>(); for (int i=0;i<ins.length();i++){ for (int j=i+1;j<=ins.length();j++){ String key = ins.substring(i,j); if (sub.containsKey(key)){ sub.put(key,sub.get(key)+1); } else { sub.put(key,1); } } } int max = 0; for (String key:sub.keySet()){ if (sub.get(key) >= 2 && key.length() > max){ max = key.length(); } } System.out.print(max); } }
cubic
23_A. You're Given a String...
CODEFORCES
import static java.util.Arrays.*; import static java.lang.Math.*; import static java.math.BigInteger.*; import java.util.*; import java.math.*; import java.io.*; public class A implements Runnable { String file = "input"; void init() throws IOException { //input = new BufferedReader(new FileReader(file + ".in")); input = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedOutputStream(System.out)); } void solve() throws IOException { String s = next(); int res = 0; int L = s.length(); for(int i = 0; i < L; i++) for(int j = i + 1; j <= L; j++) { String ss = s.substring(i, j); int k = s.indexOf(ss); //print(ss, k); if(k >= 0) { if(s.substring(k + 1).indexOf(ss) >= 0) res = max(res, j - i); } } System.out.println(res); } String next() throws IOException { if(st == null || !st.hasMoreTokens()) st = new StringTokenizer(input.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } void print(Object... o) { System.out.println(deepToString(o)); } void gcj(Object o) { String s = String.valueOf(o); out.println("Case #" + test + ": " + s); System.out.println("Case #" + test + ": " + s); } BufferedReader input; PrintWriter out; StringTokenizer st; int test; public static void main(String[] args) throws IOException { new Thread(null, new A(), "", 1 << 20).start(); } public void run() { try { init(); solve(); out.close(); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } }
cubic
23_A. You're Given a String...
CODEFORCES
//package round23; import java.io.PrintWriter; import java.io.StringReader; import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; public class A { private Scanner in; private PrintWriter out; // private String INPUT = add("a", 100); private String INPUT = ""; public void solve() { String str = in.next(); int n = str.length(); for(int k = n - 1;k >= 1;k--){ HashSet<String> set = new HashSet<String>(); for(int i = 0;i < str.length() - k + 1;i++){ if(!set.add(str.substring(i, i + k))){ out.println(k); return; } } } out.println(0); } public void run() throws Exception { in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(new StringReader(INPUT)); out = new PrintWriter(System.out); solve(); out.flush(); } public static String add(String str, int k) { StringBuilder mul = new StringBuilder(str); StringBuilder ret = new StringBuilder(); for(int i = k;i > 0;i >>= 1){ if((i & 1) == 1)ret.append(mul); mul.append(mul); } return ret.toString(); } public static void main(String[] args) throws Exception { new A().run(); } private int ni() { return Integer.parseInt(in.next()); } private static void tr(Object... o) { System.out.println(o.length == 1 ? o[0] : Arrays.toString(o)); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.*; public class Answer23A{ public static void main(String[] args){ BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); new Kai(reader).solve(); } } class Kai{ BufferedReader reader; public Kai(BufferedReader reader){ this.reader=reader; } public void solve(){ //TODO String s=read(); int max=0; for(int i=1;i<=s.length()-1;i++){ for(int j=0;j<=s.length()-i;j++){ String h=s.substring(j,j+i); for(int k=j+1;k<=s.length()-i;k++){ if(h.equals(s.substring(k,k+i))){ max=i; } } } } pln(max); } //tools////////////////////////////////////////////////// public String read(){ String s=null; try{ s=reader.readLine(); }catch(IOException e){ e.printStackTrace(); } return s; } public int[] to_i(String[] s){ int[] tmp=new int[s.length]; for(int i=0;i<s.length;i++){ tmp[i]=to_i(s[i]); } return tmp; } public long[] to_l(String[] s){ long[] tmp=new long[s.length]; for(int i=0;i<s.length;i++){ tmp[i]=to_l(s[i]); } return tmp; } public int to_i(String s){ return Integer.parseInt(s); } public long to_l(String s){ return Long.parseLong(s); } public void p(Object s){ System.out.print(s); } public void pln(Object s){ System.out.println(s); } public void debug(Object s){ System.err.print(s); } public void debugln(Object s){ System.err.println(s); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Locale; import java.util.Scanner; public class A { static { final Locale us = Locale.US; if (!Locale.getDefault().equals(us)) { Locale.setDefault(us); } } static boolean file = false; static Scanner in; static { try { in = new Scanner(file ? new FileInputStream("f:\\var\\tmp\\in.txt") : System.in); } catch (final FileNotFoundException e) { e.printStackTrace(); } } static PrintWriter out; static { try { out = file ? new PrintWriter( new FileWriter("f:\\var\\tmp\\out.txt")) : new PrintWriter( System.out); } catch (final IOException e) { e.printStackTrace(); } } /** * @param args */ public static void main(final String[] args) { try { solve(); if (file) { System.out.flush(); } if (!file) { out.flush(); } } finally { in.close(); out.close(); } } private static void solve() { final String s = in.next(); int ans = 0; for (int l = 0; l < s.length(); ++l) { for (int r = l; r < s.length(); ++r) { for (int p = l + 1; p - l + r < s.length(); ++p) { boolean ok = true; for (int q = l, qq = p; q <= r; ++q, ++qq) { if (s.charAt(q) != s.charAt(qq)) { ok = false; } } if (ok) { ans = Math.max(ans, r - l + 1); } } } } out.println(ans); } }
cubic
23_A. You're Given a String...
CODEFORCES
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Gaurav */import java.io.*; public class a23 { public static void main(String arg[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String s=br.readLine();int max=0; for(int i=0;i<s.length();i++) { for(int j=i+1;j<=s.length();j++) { String g=s.substring(i,j); //System.out.println(g); if(max<g.length()) for(int k=i+1;k<=s.length()-g.length();k++) { //System.out.println(s.substring(k,k+g.length())); if(g.compareTo(s.substring(k,k+g.length()))==0) { max=g.length(); break; } } } } System.out.println(max); } }
cubic
23_A. You're Given a String...
CODEFORCES
//contest 23. You're Given A String import java.util.*; public class ProblemA { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String input = keyboard.nextLine(); boolean con = false; for( int i = input.length()-1; i > 0 ; i--) { for ( int j = 0; j+i< input.length(); j++ ) for( int k = j+1; k+i <= input.length(); k++ ) if( input.substring(j,j+i).equals( input.substring(k,k+i) ) ) { System.out.print(i+"\n"); k = input.length()+1; j = input.length(); i = -1; con = true; } } if( con == false ) System.out.print(0+"\n"); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class A{ Scanner sc=new Scanner(System.in); void run(){ String s=sc.nextLine(); int n=s.length(); int ans=0; for(int len=1; len<n; len++){ for(int i=0; i+len<=n; i++){ String t=s.substring(i, i+len); // println(t); if(s.indexOf(t,i+1)!=-1){ ans=len; break; } } } println(ans+""); } void println(String s){ System.out.println(s); } void print(String s){ System.out.print(s); } public static void main(String[] args){ new A().run(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.Scanner; public class P23A { public P23A() { Scanner sc = new Scanner(System.in); String str = sc.next(); sc.close(); String maxStr = ""; for (int i = 0; i < str.length() - 1; i++){ for (int j = i + 1; j < str.length(); j++){ String pattern = str.substring(i, j); if (str.substring(i+1).contains(pattern) && pattern.length() > maxStr.length()){ maxStr = pattern; } } } System.out.println(maxStr.length()); } public static void main (String []args){ new P23A(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Test { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = in.readLine(); int ans = 0; for (int i = 0; i < s.length(); i++) { for (int j = i + 1; j <= s.length(); j++) { String t = s.substring(i, j); if (s.indexOf(t, i + 1)>=0) { ans = Math.max(ans, j - i); } } } System.out.println(ans); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class R023A { String str; int n; public R023A() { Scanner scanner = new Scanner(System.in); str = scanner.next(); n = str.length(); } private void process() { int length = -1; for(int i=1; i<n; i++) { Set<String> set = new HashSet<String>(); length = n - i; for(int j=0; j<=i; j++) { String sub = str.substring(j, j+length); set.add(sub); } if(set.size() < i+1) { System.out.println(length); return; } } System.out.println(0); } public static void main(String[] args) { new R023A().process(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class A { public static void main(String[] args) throws Exception{ String str = new Scanner(System.in).next(); Set<String> set = new HashSet<String>(); String max = ""; for(int l = 1; l < str.length(); ++l){ for(int i = 0; i < str.length()-l; ++i){ String substr = str.substring(i, i+l); if(!set.contains(substr) && str.indexOf(substr) != str.lastIndexOf(substr)){ set.add(substr); if(substr.length() > max.length()){ max = substr; } } } } System.out.println(max.length()); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; public class _P023A{ Scanner sc=new Scanner(System.in); int INF=1<<28; double EPS=1e-9; String s; void run(){ s=sc.nextLine(); solve(); } void solve(){ int n=s.length(); int max=0; for(int i=0; i<n; i++){ for(int j=i+1; j<n; j++){ for(int k=0; j+k<n; k++){ if(s.charAt(i+k)!=s.charAt(j+k)){ break; }else{ max=max(max, k+1); } } } } println(max+""); } void println(String s){ System.out.println(s); } void print(String s){ System.out.print(s); } void debug(Object... os){ System.err.println(Arrays.deepToString(os)); } public static void main(String[] args){ Locale.setDefault(Locale.US); new _P023A().run(); } }
cubic
23_A. You're Given a String...
CODEFORCES
/** * Mx NINJA 04:06:52 ص 14/01/2014 */ import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringBuilder line = new StringBuilder(reader.readLine()); int length = 0; for (int head = 0; head < line.length(); head++) { for (int tail = line.length() - 1; tail > head; tail--) { String subString = line.substring(head, tail); if(line.indexOf(subString,head+1)>-1){ length = Math.max(subString.length(), length); } } } System.out.println(length); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.*; public class A implements Runnable { public static void main(String[] args) { new A().run(); } class FastScanner { BufferedReader br; StringTokenizer st; boolean eof; String buf; public FastScanner(String fileName) throws FileNotFoundException { br = new BufferedReader(new FileReader(fileName)); nextToken(); } public FastScanner(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); nextToken(); } String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; break; } } String ret = buf; buf = eof ? "-1" : st.nextToken(); return ret; } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } void close() { try { br.close(); } catch (Exception e) { } } boolean isEOF() { return eof; } } FastScanner sc; PrintWriter out; public void run() { Locale.setDefault(Locale.US); try { sc = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); sc.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } } int nextInt() { return sc.nextInt(); } String nextToken() { return sc.nextToken(); } long nextLong() { return sc.nextLong(); } double nextDouble() { return sc.nextDouble(); } void solve() { String s = nextToken(); for (int len = s.length(); len >= 1; len--) { for (int i = 0; i + len <= s.length(); i++) { int cnt = 0; for (int j = 0; j + len <= s.length(); j++) { boolean ok = true; for (int k = 0; k < len; k++) { if (s.charAt(i + k) != s.charAt(j + k)) { ok = false; break; } } if (ok) { cnt++; } } if (cnt > 1) { out.println(len); return; } } } out.println(0); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.Scanner; public class GiveString23A { public static void main(String[] args) { Scanner in = new Scanner(System.in); String stroke = in.next(); char[] s = new char [stroke.length()]; for (int i=0;i<stroke.length();i++) s[i]=stroke.charAt(i); int dlina = 0; for (int i=0;i<s.length-1;i++) for (int j=i+1;j<s.length;j++) for (int k=0;k<(s.length-j);k++) if (s[i]==s[j]) { int ik=i+k; int jk = j+k; if (s[ik]==s[jk]) { if (dlina<k+1) dlina=k+1; } else break; } System.out.println(dlina); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class P023A { public static void main(String[] args) { Scanner inScanner = new Scanner(System.in); String string = inScanner.next(); int n = string.length(); for (int l = n - 1; l > 0; l--) { Set<String> seen = new HashSet<String>(); for (int i = 0; i < n - l + 1; i++) { String subString = string.substring(i, i + l); if (seen.contains(subString)) { System.out.println(l); return; } seen.add(subString); } } System.out.println("0"); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.HashSet; import java.util.InputMismatchException; import java.util.Set; /** * @author Egor Kulikov ([email protected]) * Created on 14.03.2010 */ public class TaskA implements Runnable { private InputReader in; private PrintWriter out; public static void main(String[] args) { new Thread(new TaskA()).start(); // new Template().run(); } public TaskA() { // String id = getClass().getName().toLowerCase(); // try { // System.setIn(new FileInputStream(id + ".in")); // System.setOut(new PrintStream(new FileOutputStream(id + ".out"))); // System.setIn(new FileInputStream("input.txt")); // System.setOut(new PrintStream(new FileOutputStream("output.txt"))); // } catch (FileNotFoundException e) { // throw new RuntimeException(); // } in = new InputReader(System.in); out = new PrintWriter(System.out); } public void run() { // int numTests = in.readInt(); // for (int testNumber = 0; testNumber < numTests; testNumber++) { // out.print("Case " + (testNumber + 1) + ": "); // } String s = in.readString(); Set<String> m = new HashSet<String>(); int ans = 0; for (int i = 0; i < s.length(); i++) { for (int j = i + 1; j <= s.length(); j++) { if (m.contains(s.substring(i, j))) ans = Math.max(ans, j - i); else m.add(s.substring(i, j)); } } out.print(ans); out.close(); } private static class InputReader { private InputStream stream; private byte[] buf = new byte[1000]; private int curChar, numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuffer buf = new StringBuffer(); int c = read(); while (c != '\n' && c != -1) { buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) return readLine(); else return readLine0(); } public BigInteger readBigInteger() { try { return new BigInteger(readString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.text.DecimalFormat; import java.util.Scanner; public class A { public static void main(String[] args){ try{ Scanner scanner = new Scanner(System.in); String in = scanner.next(); int max = 0; for(int j=0;j<in.length()-1;j++){ for(int i=j;i<in.length();i++){ if(in.indexOf(in.substring(j, i)) != in.lastIndexOf(in.substring(j, i)) && (i-j)>max){ max = i-j; } } } System.out.println(max); }catch(Exception e){ e.printStackTrace(); } } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class Solution23A { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE")!=null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException{ if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException{ while(!tok.hasMoreTokens()){ tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException{ return Integer.parseInt(readString()); } long readLong() throws IOException{ return Long.parseLong(readString()); } double readDouble() throws IOException{ return Double.parseDouble(readString()); } public static void main(String[] args){ new Solution23A().run(); } public void run(){ try{ long t1 = System.currentTimeMillis(); init(); solve(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = "+(t2-t1)); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } void solve() throws IOException{ char[] t = readString().toCharArray(); int max = 0; for(int i = 0; i < t.length; i++){ for(int j = i + 1; j < t.length; j++){ for(int k = 0; k < t.length - j; k++){ if(t[i+k] == t[j+k]) max = max(max, k+1); else break; } } } out.println(max); } int ModExp(int a, int n, int mod){ int res = 1; while (n!=0) if ((n & 1) != 0) { res = (res*a)%mod; --n; } else { a = (a*a)%mod; n >>= 1; } return res; } static class Utils { private Utils() {} public static void mergeSort(int[] a) { mergeSort(a, 0, a.length - 1); } private static void mergeSort(int[] a, int leftIndex, int rightIndex) { final int MAGIC_VALUE = 50; if (leftIndex < rightIndex) { if (rightIndex - leftIndex <= MAGIC_VALUE) { insertionSort(a, leftIndex, rightIndex); } else { int middleIndex = (leftIndex + rightIndex) / 2; mergeSort(a, leftIndex, middleIndex); mergeSort(a, middleIndex + 1, rightIndex); merge(a, leftIndex, middleIndex, rightIndex); } } } private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) { int length1 = middleIndex - leftIndex + 1; int length2 = rightIndex - middleIndex; int[] leftArray = new int[length1]; int[] rightArray = new int[length2]; System.arraycopy(a, leftIndex, leftArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = leftArray[i++]; } else { a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++]; } } } private static void insertionSort(int[] a, int leftIndex, int rightIndex) { for (int i = leftIndex + 1; i <= rightIndex; i++) { int current = a[i]; int j = i - 1; while (j >= leftIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } } boolean isPrime(int a){ for(int i = 2; i <= sqrt(a); i++) if(a % i == 0) return false; return true; } static double distance(long x1, long y1, long x2, long y2){ return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } static long gcd(long a, long b){ if(min(a,b) == 0) return max(a,b); return gcd(max(a, b) % min(a,b), min(a,b)); } static long lcm(long a, long b){ return a * b /gcd(a, b); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.StringTokenizer; /** * * @author Prateep */ public class JavaApplication1 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB{ public void solve(int testNumber, InputReader in, PrintWriter out){ String base = in.next(); for (int len=base.length()-1;len>=1;len--) for (int i=0;i<base.length()-len+1;i++) for (int j=i+1;j<base.length()-len+1;j++) if (base.substring(i,i+len).equals(base.substring(j,j+len))){ out.println(len); return; } out.println(0); } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.*; public class A implements Runnable { BufferedReader br; StringTokenizer in; PrintWriter out; public static void main(String[] args) { new Thread(new A()).start(); } public String nextToken() throws IOException { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public void solve() throws IOException { String s = nextToken(); int max = 0; for(int i = 0 ; i < s.length(); i++) for(int j = i+1 ; j < s.length(); j ++){ String sub = s.substring(i, j); int kv = 0; for(int k = 0 ; k<= s.length() - sub.length(); k ++){ boolean ok = true; for(int g = 0 ; g < sub.length(); g ++) if (sub.charAt(g) != s.charAt(g+k)){ ok = false; break; } if (ok) kv ++; } if (kv > 1) max = Math.max(max, sub.length()); } out.println(max); } public void run() { try { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); // br = new BufferedReader(new FileReader("input.txt")); // out = new PrintWriter("output.txt"); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; import java.util.regex.*; import java.text.*; import java.math.*; import java.io.*; public class Mulitple { public static void main(String[] args) throws IOException { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); String s = r.readLine(); System.out.println(num(s)); } public static int num(String s) { int answer = 0; Set<String> set = new HashSet<String>(); for(int j = s.length()-1; j>=1; j--) { for(int i = 0; i<s.length()-j+1; i++) { if(set.contains(s.substring(i,i+j))) { return s.substring(i,i+j).length(); } else { set.add(s.substring(i,i+j)); } } } return 0; } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.lang.Math.*; public class Main { //StreamTokenizer in; BufferedReader in; PrintWriter out; public static void main(String[] args) throws IOException { new Main().run(); } void run() throws IOException { //in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); String s = in.readLine(); int n = s.length(); for(int len = n; len > 0; len--) { for(int i = 0; i + len <= n; i++) { for(int j = 0; j + len <= n; j++) if(i != j) { boolean f = true; for(int k = 0; k < len; k++) if(s.charAt(i + k) != s.charAt(j + k)) { f = false; break; } if(f) { out.println(len); out.flush(); return; } } } } out.println(0); out.flush(); } void solve() throws IOException { } //int ni() throws IOException { in.nextToken(); return (int) in.nval; } }
cubic
23_A. You're Given a String...
CODEFORCES
import static java.lang.Math.max; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class GivenString implements Runnable { public static void main(String[] args) throws Exception { new GivenString().run(); } private void solve() throws Exception { String s = nextToken(); int len = s.length(); KMP kmp = new KMP(); int r = 0; for (int i = 0; i < len; i++) { for (int j = i + 1; j <= len; j++) { String cur = s.substring(i, j); int count = kmp.search(s, cur); if (count >= 2) r = max(r, cur.length()); } } out.println(r); } class KMP { public int search(String text, String pattern) { int count = 0; int n = text.length(), m = pattern.length(), matchPoint = -1; char pat[] = pattern.toCharArray(), t[] = text.toCharArray(); int p[] = prefixTable(pattern); int j = 0; for (int i = 0; i < n; i++) { while (j > 0 && pat[j] != t[i]) j = p[j - 1]; if (pat[j] == t[i]) j++; if (j == m) { matchPoint = i - m + 1; j = p[j - 1]; count++; } } return count; } private int[] prefixTable(String pat) { int m = pat.length(), p[] = new int[m]; char s[] = pat.toCharArray(); int j = 0; for (int i = 1; i < m; i++) { while (j > 0 && s[j] != s[i]) j = p[j - 1]; if (s[j] == s[i]) j++; p[i] = j; } return p; } } // -------------- Input/Output routines below ---------------// private BufferedReader in; PrintWriter out; StringTokenizer tokenizer; public void run() { // String problem = this.getClass().getName(); try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedOutputStream(System.out)); solve(); out.flush(); in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); // System.exit(1); } } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main{ private static Parser in; private static PrintWriter out; public static void main(String[] args){ in = new Parser(System.in); out = new PrintWriter(System.out); // // char[] ccc = new char[1]; // String ddd = "abcdef"; // ddd.getChars(2, 3, ccc, 0); // // String sssss = new String(ccc); // // System.out.println(sssss); // String s = in.nextString(100); int len = 0; String ss = ""; l:for (int i = 1; i<=s.length(); i++){ for(int j = 0; j+i<=s.length();j++){ char[] c = new char[i]; char[] cc = new char[i]; s.getChars(j, j+i, c, 0); String sss = new String(c); //System.out.println(sss); for(int k = j+1; k+i<=s.length();k++){ s.getChars(k, k+i, cc, 0); String ssss = new String(cc); if(sss.equals(ssss)) {len = i; continue l;} } } } System.out.println(len); // out.flush(); } } class Parser { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Parser(InputStream in) { din = new DataInputStream(in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextString(int size) { byte[] ch = new byte[size]; int point = 0; try { byte c = read(); while (c == ' ' || c == '\n' || c=='\r') c = read(); while (c != ' ' && c != '\n' && c!='\r') { ch[point++] = c; c = read(); } } catch (Exception e) {} return new String(ch,0,point); } public int nextInt() { int ret = 0; boolean neg; try { byte c = read(); while (c <= ' ') c = read(); neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; } catch (Exception e) {} return ret; } public long nextLong() { long ret = 0; boolean neg; try { byte c = read(); while (c <= ' ') c = read(); neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; } catch (Exception e) {} return ret; } private void fillBuffer() { try { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); } catch (Exception e) {} if (bytesRead == -1) buffer[0] = -1; } private byte read() { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } }
cubic
23_A. You're Given a String...
CODEFORCES
/** * Created by IntelliJ IDEA. * User: shakhov * Date: 15.06.2011 * Time: 15:22:46 * To change this template use File | Settings | File Templates. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class CodeForces { public void solve() throws IOException { String s = nextToken(); Set<String> set = new HashSet<String>(); int counter = 0; for (int i = 0, l = s.length(); i < l; i++) { for (int j = i + 1; j < l; j++) { String subst = s.substring(i, j); if (!set.contains(subst)) { set.add(subst); if (counts(s.toCharArray(), subst.toCharArray()) > 1) { counter = Math.max(counter, subst.length()); } } } } writer.print(counter); } private int counts(char[] s, char[] r) { int l = s.length; int rl = r.length; int arr[] = new int[26]; Arrays.fill(arr, rl); for (int i = rl - 2; i > -1; i--) { int margin = (r[i] - 'a'); if (arr[margin] == rl) { arr[margin] = rl - i - 1; } } //search int sp = 0; int counter = 0; while (sp <= l - rl) { int oldsp = sp; for (int i = rl - 1; i > -1; i--) { if (r[i] != s[sp + i]) { if (i == rl - 1) { sp += arr[s[sp + i] - 'a']; } else { sp++; } break; } } if (oldsp == sp) { counter++; sp++; } } return counter; } public static void main(String[] args) { new CodeForces().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); //reader = new BufferedReader(new FileReader("LifeWithoutZeros.in")); tokenizer = null; writer = new PrintWriter(System.out); //writer = new PrintWriter(new BufferedWriter(new FileWriter("LifeWithoutZeros.out"))); //long t=new Date().getTime(); solve(); //writer.println(t-new Date().getTime()); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.Scanner; public class p23a { public static void main(String[] args) { Scanner in = new Scanner(System.in); char[] x = in.next().toCharArray(); int min = 0; int max = x.length; while(true) { if(max-min == 1) break; int mid = (max+min)/2; boolean eq = false; for (int i = 0; i <= x.length-mid; i++) { for (int j = 0; j <= x.length-mid; j++) { if(j == i) continue; eq = true; for (int k = 0; k < mid; k++) { if(x[i+k] != x[j+k]) { eq = false; break; } } if(eq) break; } if(eq) break; } if(eq) { min = mid; } else { max = mid; } } System.out.println(min); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class A23 implements Runnable { private void Solution() throws IOException { String s = in.readLine(); int n = s.length(), ans = 0; for (int i = 0; i < n; i ++) { for (int j = i; j < n; j ++) { for (int k = i+1; k <= n; k ++) { for (int g = k; g <= n; g ++) { if (s.substring(i,j).equals(s.substring(k,g))) { int l = s.substring(i,j).length(); ans = Math.max(ans, l); } } } } } System.out.println(ans); } public static void main(String args[]) { new A23().run(); } BufferedReader in; StringTokenizer tokenizer; public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; Solution(); in.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(in.readLine()); return tokenizer.nextToken(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class CodeF { static class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String nextLine() { try { return br.readLine(); } catch(Exception e) { throw(new RuntimeException()); } } public String next() { while(!st.hasMoreTokens()) { String l = nextLine(); if(l == null) return null; st = new StringTokenizer(l); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] res = new int[n]; for(int i = 0; i < res.length; i++) res[i] = nextInt(); return res; } public long[] nextLongArray(int n) { long[] res = new long[n]; for(int i = 0; i < res.length; i++) res[i] = nextLong(); return res; } public double[] nextDoubleArray(int n) { double[] res = new double[n]; for(int i = 0; i < res.length; i++) res[i] = nextDouble(); return res; } public void sortIntArray(int[] array) { Integer[] vals = new Integer[array.length]; for(int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for(int i = 0; i < array.length; i++) array[i] = vals[i]; } public void sortLongArray(long[] array) { Long[] vals = new Long[array.length]; for(int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for(int i = 0; i < array.length; i++) array[i] = vals[i]; } public void sortDoubleArray(double[] array) { Double[] vals = new Double[array.length]; for(int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for(int i = 0; i < array.length; i++) array[i] = vals[i]; } public String[] nextStringArray(int n) { String[] vals = new String[n]; for(int i = 0; i < n; i++) vals[i] = next(); return vals; } Integer nextInteger() { String s = next(); if(s == null) return null; return Integer.parseInt(s); } int[][] nextIntMatrix(int n, int m) { int[][] ans = new int[n][]; for(int i = 0; i < n; i++) ans[i] = nextIntArray(m); return ans; } } static int[] compute_prefix_function(char[] p) { int[] pi = new int[p.length]; pi[0] = -1; int k = -1; for (int i = 1; i < p.length; i++) { while (k >= 0 && p[k + 1] != p[i]) k = pi[k]; if (p[k + 1] == p[i]) k++; pi[i] = k; } return pi; } static boolean KMP_Matcher(String pattern, String text) { char[] p = pattern.toCharArray(); char[] t = text.toCharArray(); int[] pi = compute_prefix_function(p); int q = -1; int cuenta = 0; for (int i = 0; i < text.length(); i++) { while (q >= 0 && p[q + 1] != t[i]) q = pi[q]; if (p[q + 1] == t[i]) q++; if (q == p.length - 1) { cuenta++; q = pi[q]; } } return cuenta >= 2; } public static void main(String[] args) { Scanner sc = new Scanner(); String entrada = sc.next(); int mejor = 0; for(int i = 0; i < entrada.length(); i++) { for(int j = i + 1; j <= entrada.length(); j++) { String sub = entrada.substring(i, j); if(KMP_Matcher(sub, entrada)) mejor = Math.max(j - i, mejor); } } System.out.println(mejor); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.*; import java.util.*; import static java.lang.Math.*; public class ProblemA_23 { final boolean ONLINE_JUDGE=System.getProperty("ONLINE_JUDGE")!=null; BufferedReader in; PrintWriter out; StringTokenizer tok=new StringTokenizer(""); void init() throws FileNotFoundException{ if (ONLINE_JUDGE){ in=new BufferedReader(new InputStreamReader(System.in)); out =new PrintWriter(System.out); } else{ in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException{ while(!tok.hasMoreTokens()){ tok=new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException{ return Integer.parseInt(readString()); } public static void main(String[] args){ new ProblemA_23().run(); } public void run(){ try{ long t1=System.currentTimeMillis(); init(); solve(); out.close(); long t2=System.currentTimeMillis(); System.err.println("Time = "+(t2-t1)); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } void solve() throws IOException{ String s = readString(); for (int length = s.length() - 1; length > 0; length--){ for (int i = 0; i < s.length() - length; i++){ if (s.lastIndexOf(s.substring(i, i + length)) > i){ out.print(length); return; } } } out.print(0); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class A { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = new String(in.readLine()); int len=s.length(); int ans=0; for (int i=0;i<len-1;i++) { for (int j=i+1;j<len;j++) { int score=0; boolean flag=true; for (int k=0;k+j<len && flag;k++) { if (s.charAt(i+k)==s.charAt(j+k)) { score++; } else { flag=false; } } if (score>ans) { ans=score; } } } System.out.println(ans); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; public class P023A { public static void main(String[] args) { Scanner in = new Scanner(System.in); String line = in.next(); HashSet<String> hash = new HashSet<String>(); int ans = 0; for (int len = line.length()-1; len > 0; --len) { for (int i = 0; i + len <= line.length(); ++i) { String sub = line.substring(i, i+len); if (hash.contains(sub)) { ans = Math.max(ans, sub.length()); } hash.add(sub); } } System.out.println(ans); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main { boolean test = false; PrintWriter pw = new PrintWriter(System.out); InputStreamReader inp = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(inp); private String[] inData = {"zzz" }; static int id = -1; public String readLine() { id++; if (test) return inData[id]; else try{ return in.readLine(); } catch( Exception e){ e.printStackTrace(); } return ""; } private void solve() { String readLine = readLine(); int best = 0; for (int i = 0; i < readLine.length(); i++) { for (int j = i; j < readLine.length(); j++) { String substring = readLine.substring(i,j+1); String remainString = readLine.substring(i+1); if(remainString.contains(substring)){ if(substring.length() > best){ best = substring.length(); } } } } System.out.println(best); } public static void main(String args[]) throws Exception { new Main().solve(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.Scanner; public class x23A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String input = sc.next(); //input+="3"; int longest=0; if(input.length()==1){ System.out.println(0); System.exit(0); } if(input.length()==2){ if(input.charAt(0)==input.charAt(1)){ System.out.println(1); System.exit(0); } else{ System.out.println(0); System.exit(0);} } for(int a=0;a<input.length()-1;a++){ for(int b=a+1;b<input.length();b++){ for(int c=1;(c+b)<input.length()+1;c++){ // System.out.printf("%s %s %d\n", input.substring(a,a+c), input.substring(b,b+c), input.substring(a,a+c).compareTo(input.substring(b,b+c))); if(input.substring(a,a+c).compareTo(input.substring(b,b+c))==0) if(longest<c)longest=c; } } } System.out.println(longest); } }
cubic
23_A. You're Given a String...
CODEFORCES
//package codeforces.br23; import java.io.*; /** * User: Kandy * Date: 12.07.2010 * Time: 21:51:52 */ public class ProblemA { public void solve() { boolean oj = true; try { Reader reader = oj ? new InputStreamReader(System.in) : new FileReader("A.in"); Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("A.out"); BufferedReader br = new BufferedReader(reader); StreamTokenizer st = new StreamTokenizer(reader); PrintWriter out = new PrintWriter(writer); String s = br.readLine(); int n = s.length(); int max = 0; for (int i = 0; i < n; i++) for (int j = i; j < n; j++) { int len = j - i + 1; int count = 0; for (int k = 0; k < n - len + 1; k++) { boolean eq = true; for(int l = 0; l < len;l++) { if (s.charAt(i + l) != s.charAt(k + l)) { eq = false; break; } } if (eq) { count++; } } if (count >= 2 && len > max) { max = len; } } out.printf("%d", max); br.close(); out.close(); reader.close(); writer.close(); } catch (Exception ex) { ex.printStackTrace(); } finally { } } public static void main(String[] args) { ProblemA f = new ProblemA(); f.solve(); } private class MyTokenizer { private String s; private int cur; public MyTokenizer(String s) { this.s = s; cur = 0; } public void skip() { while (cur < s.length() && (s.charAt(cur) == ' ' || s.charAt(cur) == '\n')) { cur++; } } public double getNum() { skip(); String snum = ""; while (cur < s.length() && (s.charAt(cur) >= '0' && s.charAt(cur) <= '9' || s.charAt(cur) == '.' || s.charAt(cur) == '-')) { snum += s.charAt(cur); cur++; } return Double.valueOf(snum); } public String getString() { skip(); String s2 = ""; while (cur < s.length() && (((s.charAt(cur) >= 'a' && s.charAt(cur) <= 'z')) || ((s.charAt(cur) >= 'A' && s.charAt(cur) <= 'Z')))) { s2 += s.charAt(cur); cur++; } return s2; } public char getCurrentChar() throws Exception { if (cur < s.length()) return s.charAt(cur); else throw new Exception("Current character out of string length"); } public void moveNextChar() { if (cur < s.length()) cur++; } public boolean isFinished() { return cur >= s.length(); } } }
cubic
23_A. You're Given a String...
CODEFORCES
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import com.sun.org.apache.xalan.internal.xsltc.dom.BitArray; import java.io.DataOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.BitSet; import java.util.Hashtable; import java.util.Iterator; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author abdelrahman */ public class p1 { /** * @param args the command line arguments */ public static void main(String[] args) { new p1().run(); } private void run() { try { // TODO code application logic here Scanner scanner = new Scanner(System.in); String in = scanner.next(); Hashtable<String, Boolean> tmp = new Hashtable<String, Boolean>(); int sol = 0; for (int i = 0; i < in.length(); i++) { for (int j = i + 1; j <= in.length(); j++) { String str = in.substring(i, j); if (tmp.containsKey(str)) { if (tmp.get(str)) { if(str.length() > sol) sol=str.length(); boolean tmp1 = tmp.remove(str); tmp.put(str, false); } } else { tmp.put(str, Boolean.TRUE); } } } System.out.println(sol); } catch (Exception ex) { } } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.BufferedReader; import static java.lang.Math.*; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class CFC23A implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok; public static void main(String[] args) { new Thread(new CFC23A()).start(); } void solve() throws IOException { int res = 0; String str = nextToken(); for(int i = 0; i < str.length(); ++i) for(int j = i + 1; j <= str.length(); ++j) if(isOk(str.substring(i, j), str)) res = max(res, j - i); out.println(res); } private boolean isOk(String substring, String str) { int from = 0, kol = 0; while(str.indexOf(substring, from) != -1 && kol < 2) { ++kol; from = str.indexOf(substring, from) + 1; } return kol >= 2; } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); // in = new BufferedReader(new FileReader(new File("input.txt"))); out = new PrintWriter(System.out); //out = new PrintWriter(new File("output.txt")); solve(); out.flush(); out.close(); in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } String nextLine() throws IOException { tok = null; return in.readLine(); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } String nextToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } }
cubic
23_A. You're Given a String...
CODEFORCES
/** * Mx 04:06:52 ص 14/01/2014 */ import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringBuilder line = new StringBuilder(reader.readLine()); int length = 0; for (int head = 0; head < line.length(); head++) { for (int tail = line.length() - 1; tail > head; tail--) { String subString = line.substring(head, tail); if(line.indexOf(subString,head+1)>-1){ length = Math.max(subString.length(), length); } } } System.out.println(length); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.*; import java.util.*; import java.lang.*; public class A { private BufferedReader in; private StringTokenizer st; void solve() throws IOException{ int len = 0; String x = next(); HashSet<String> h = new HashSet<String>(); for (int i = 0; i < x.length(); i++) { for (int j = i+1; j <= x.length(); j++) { String y = x.substring(i,j); if(h.contains(y)){ if(y.length()>len) len = y.length(); } else h.add(y); } } System.out.println(len); } A() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); eat(""); solve(); } private void eat(String str) { st = new StringTokenizer(str); } String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } eat(line); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } public static void main(String[] args) throws IOException { new A(); } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class Main{ private static int[] T; public static void main(String[] args){ Scanner in = new Scanner(System.in); char[] input = in.nextLine().toCharArray(); int length = input.length; int max = 0; for(int i=0; i<length; i++){ char[] subString = Arrays.copyOfRange(input, 1, input.length); int temp = solve(input, subString); if(temp > max) max = temp; input = Arrays.copyOfRange(input, 1, input.length); } System.out.println(max); } private static int solve(char[] P, char[] S) { T = new int[P.length+1]; preKmp(P, P.length, T); int max = 0; int i = 0, j = 0; while (j < S.length) { while (i > -1 && (P[i] != S[j])) i = T[i]; i++; j++; if ( i > max) max = i; if (i >= P.length) { i = T[i]; } } return max; } private static void preKmp(char[] x, int m, int[] kmpNext) { int i = 0, j = kmpNext[0] = -1; while (i < m-1) { while (j > -1 && x[i] != x[j]) j = kmpNext[j]; i++; j++; if (x[i] == x[j]) kmpNext[i] = kmpNext[j]; else kmpNext[i] = j; } } }
cubic
23_A. You're Given a String...
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); EPhoenixAndComputers solver = new EPhoenixAndComputers(); solver.solve(1, in, out); out.close(); } static class EPhoenixAndComputers { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(), m = in.nextInt(); ModInt mod = new ModInt(m); int[] factorial = new int[n + 1]; int[][] binomial = new int[n + 1][n + 1]; int[] two = new int[n + 1]; for (int i = 0; i <= n; i++) { binomial[i][0] = 1; for (int j = 1; j <= i; j++) binomial[i][j] = mod.add(binomial[i - 1][j], binomial[i - 1][j - 1]); factorial[i] = i == 0 ? 1 : mod.multiply(factorial[i - 1], i); two[i] = i == 0 ? 1 : mod.multiply(2, two[i - 1]); } int[][] dp = new int[n + 1][n + 1]; dp[0][0] = 1; int answer = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { for (int x = 1; x <= i; x++) { dp[i][j] = mod.add(dp[i][j], mod.multiply(binomial[i][x], mod.multiply(two[x - 1], dp[i - x][j - 1]))); } } } for (int k = 0; k < n; k++) { answer = mod.add(answer, dp[n - k][k + 1]); } out.println(answer); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void println(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } writer.print('\n'); } public void close() { writer.close(); } } static class ModInt { int modulo; public ModInt(int m) { modulo = m; } public int add(int x, int y) { x += y; if (x >= modulo) x -= modulo; return x; } public int multiply(int x, int y) { return (int) ((x * 1L * y) % modulo); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
cubic
1515_E. Phoenix and Computers
CODEFORCES
import java.io.*; import java.util.*; public class CF1515E{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int md = sc.nextInt(); int k = (n + 1) / 2; int[][] dp = new int[k + 1][n + 1]; dp[0][0] = 1; for (int h = 1; h <= k; h++) for (int l = h; l <= n - h + 1; l++) dp[h][l] = (int) ((dp[h][l - 1] * 2L + dp[h - 1][l - 1]) * h % md); int ans = 0; for (int h = 1; h <= k; h++) ans = (ans + dp[h][n - h + 1]) % md; System.out.println(ans); } }
cubic
1515_E. Phoenix and Computers
CODEFORCES
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), md = sc.nextInt(); int k = (n + 1) / 2;int ans = 0; int[][] dp = new int[k + 1][n + 1];dp[0][0] = 1; for (int h = 1; h <= k; h++) for (int l = h; l <= n - h + 1; l++) dp[h][l] = (int) ((dp[h][l - 1] * 2L + dp[h - 1][l - 1]) * h % md); for (int h = 1; h <= k; h++) ans = (ans + dp[h][n - h + 1]) % md; System.out.println(ans); } }
cubic
1515_E. Phoenix and Computers
CODEFORCES
import java.io.*; import java.util.*; /* polyakoff */ public class Main { static FastReader in; static PrintWriter out; static Random rand = new Random(); static final int oo = (int) 1e9 + 10; static final long OO = (long) 1e18 + 10; static final int MOD = (int) 1e9 + 7; static int M; static long[] f, fi, two; static long pow(long a, long b) { long res = 1; while (b != 0) { if ((b & 1) == 1) res = (res * a) % M; a = (a * a) % M; b >>= 1; } return res; } static long C(int n, int k) { return f[n] * fi[k] % M * fi[n - k] % M; } static void prepare(int n) { f = new long[n]; f[0] = 1; for (int i = 1; i < n; i++) { f[i] = (f[i - 1] * i) % M; } fi = new long[n]; for (int i = 0; i < n; i++) { fi[i] = pow(f[i], M - 2); } two = new long[n]; two[0] = 1; for (int i = 1; i < n; i++) { two[i] = (two[i - 1] * 2) % M; } } static void solve() { int n = in.nextInt(); M = in.nextInt(); prepare(n + 10); long[][] dp = new long[n + 1][n]; for (int i = 1; i <= n; i++) { dp[i][1] = two[i - 1]; } for (int i = 1; i <= n; i++) { for (int j = 2; j < n; j++) { for (int k = 1; k <= i - 2; k++) { if (k < j - 2) continue; int len = i - k - 1; dp[i][j] = (dp[i][j] + dp[k][j - 1] * two[len - 1] % M * C(k - (j - 2) + len, len) % M) % M; } } } long ans = 0; for (int j = 1; j < n; j++) { ans = (ans + dp[n][j]) % M; } out.println(ans); } public static void main(String[] args) { in = new FastReader(); out = new PrintWriter(System.out); int t = 1; // t = in.nextInt(); while (t-- > 0) { solve(); } out.flush(); out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; FastReader() { this(System.in); } FastReader(String file) throws FileNotFoundException { this(new FileInputStream(file)); } FastReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } String nextLine() { String line; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } } }
cubic
1515_E. Phoenix and Computers
CODEFORCES
import java.io.*; import java.math.BigInteger; import java.util.*; /** @author KhanhNguyenn */ public class C{ public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } // main solver // sub-problem: 2 orders to merge together x1,x2,..,xn and y1,y2,..,ym // Dựa vào bài toán chia kẹp Euler, ta chứng minh dc số cách merge là // (m+n)C(m-1)=(m+n)C(n+1) static class Task{ int M; public void solve(InputReader in, PrintWriter out) { int n= in.nextInt(); M= in.nextInt(); if(n<=1){ out.println(n); return; } int[] pow2= new int[n+1]; pow2[0]=1; for(int i=1;i<=n;i++) pow2[i]=mul(2,pow2[i-1]); // calculate nCk // nCk= (n-1)C(k-1)+ (n-1)Ck int[][] Ckn= new int[n+1][n+1]; for(int i=0;i<=n;i++){ Ckn[i][i]=1; Ckn[0][i]=1; for(int j=i-1;j>=1;j--){ Ckn[j][i]= add(Ckn[j-1][i-1],Ckn[j][i-1]); } } int ans=0; int[][] dp= new int[n+1][n+1]; dp[1][1]=1; //dp[i][j]: number of ways to turn on first i , using j manually for(int i=2;i<=n;i++){ dp[i][i]= pow2[i-1]; for(int j=1;j<=i-1;j++){ for(int k=1;k<=j;k++){ dp[i][j]= add(dp[i][j],mul(mul(pow2[k-1],dp[i-k-1][j-k]),Ckn[k][j])); } } } for(int i=0;i<=n;i++) ans= add(ans,dp[n][i]); out.println(ans); } public int add(int a, int b){ a+=b; if(a>=M) a-=M; return a; } public int mul(int a, int b){ long res= (long)a*(long)b; res %=M; return (int)res; } } static class Pair { public String x; public int y; public Pair(String x, int y){ this.x = x; this.y=y; } // @Override // public int compareTo(Pair o) { // if (this.x > o.x){ // return 1; // } // else if (this.x < o.x){ // return -1; // } // else{ // return Integer.compare(this.y, o.y); // } // } } // fast input reader class; static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public String nextToken() { while (st == null || !st.hasMoreTokens()) { String line = null; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble(){ return Double.parseDouble(nextToken()); } public long nextLong(){ return Long.parseLong(nextToken()); } } }
cubic
1515_E. Phoenix and Computers
CODEFORCES
import java.util.*; import java.lang.*; // StringBuilder uses java.lang public class mC { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder st = new StringBuilder(); int t = 1; for (int test = 0; test < t; test++) { int n = sc.nextInt(); int MOD = sc.nextInt(); long[] factorial = new long[1000]; long[] powerOfTwo = new long[1000]; factorial[0]=1; powerOfTwo[0]=1; for (int i=1;i<1000;i++) { factorial[i]=i*factorial[i-1]; factorial[i] %= MOD; powerOfTwo[i]=2*powerOfTwo[i-1]; if (powerOfTwo[i]>=MOD) { powerOfTwo[i]-=MOD; } } long[] oneOverFactorial = new long[500]; oneOverFactorial[0]=1; oneOverFactorial[1]=1; for (int i=2;i<450;i++) { oneOverFactorial[i] = fastPow(factorial[i],MOD-2,MOD); } long dp[][] = new long[n+3][n+3]; // first value number of computers considered // second value number of computers manually on dp[1][1]=1; for (int i=2;i<=n;i++) { // from left to right, over first i computers dp[i][i]=powerOfTwo[i-1]; for (int j=1;j<i-1;j++) { // number of computers turned on manually for (int k=1;k<=j;k++) { // want to add dp[j][k]*factorial[k+(i-j-1)]*oneOverFactorial[k]*oneOverFactorial[i-j-1]*powerOfTwo[i-j-2] long add = dp[j][k]*factorial[k+(i-j-1)]; add %= MOD; add *= oneOverFactorial[k]; add %= MOD; add *= oneOverFactorial[i-j-1]; add %= MOD; add *= powerOfTwo[i-j-2]; add %= MOD; dp[i][k+(i-j-1)]+=add; dp[i][k+(i-j-1)]%=MOD; } } } long ans = 0; for (int i=1;i<=n;i++) { ans+=dp[n][i]; } ans %= MOD; System.out.println(ans); } //System.out.print(st.toString()); } public static int goodLeft(int n, int[] p) { // i.e. ... 4 5 6 int begin = 0; for (int i=0;i<n;i++) { if (n-i==p[n-i-1]) { begin++; } else { break; } } return begin; } public static int goodRight(int n, int[] p) { // i.e. 6 5 4 ... int end = 0; for (int i=n-1;i>=0;i--) { if (i==p[i]) { end++; } else { break; } } return end; } public static int findNthInArray(int[] arr,int val,int start,int o) { if (o==0) { return start-1; } else if (arr[start] == val) { return findNthInArray(arr,val,start+1,o-1); } else { return findNthInArray(arr,val,start+1,o); } } public static ArrayList<Integer> dfs(int at,ArrayList<Integer> went,ArrayList<ArrayList<Integer>> connect) { for (int i=0;i<connect.get(at).size();i++) { if (!(went.contains(connect.get(at).get(i)))) { went.add(connect.get(at).get(i)); went=dfs(connect.get(at).get(i),went,connect); } } return went; } public static int[] bfs (int at, int[] went, ArrayList<ArrayList<Integer>> queue, int numNodes, ArrayList<ArrayList<Integer>> connect) { if (went[at]==0) { went[at]=queue.get(numNodes).get(1); for (int i=0;i<connect.get(at).size();i++) { if (went[connect.get(at).get(i)]==0) { ArrayList<Integer> temp = new ArrayList<>(); temp.add(connect.get(at).get(i)); temp.add(queue.get(numNodes).get(1)+1); queue.add(temp); } } } if (queue.size()==numNodes+1) { return went; } else { return bfs(queue.get(numNodes+1).get(0),went, queue, numNodes+1, connect); } } public static long fastPow(long base,long exp,long mod) { if (exp==0) { return 1; } else { if (exp % 2 == 1) { long z = fastPow(base,(exp-1)/2,mod); return ((((z*base) % mod) * z) % mod); } else { long z = fastPow(base,exp/2,mod); return ((z*z) % mod); } } } public static int fastPow(int base,long exp) { if (exp==0) { return 1; } else { if (exp % 2 == 1) { int z = fastPow(base,(exp-1)/2); return ((((z*base)) * z)); } else { int z = fastPow(base,exp/2); return ((z*z)); } } } public static int firstLarger(long val,ArrayList<Long> ok,int left,int right) { if (ok.get(right)<=val) { return -1; } if (left==right) { return left; } else if (left+1==right) { if (val<ok.get(left)) { return left; } else { return right; } } else { int mid = (left+right)/2; if (ok.get(mid)>val) { return firstLarger(val,ok,left,mid); } else { return firstLarger(val,ok,mid+1,right); } } } public static int binSearchArr(long val,ArrayList<Integer> ok,long[] arr,int left,int right) { if (arr[ok.get(right)]<=val) { return -1; } if (left==right) { return left; } else if (left+1==right) { if (val<arr[ok.get(left)]) { return left; } else { return right; } } else { int mid = (left+right)/2; if (arr[ok.get(mid)]>val) { return binSearchArr(val,ok,arr,left,mid); } else { return binSearchArr(val,ok,arr,mid+1,right); } } } public static long gcd(long a, long b) { if (b>a) { return gcd(b,a); } if (b==0) { return a; } if (a%b==0) { return b; } else { return gcd(b,a%b); } } }
cubic
1515_E. Phoenix and Computers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(); int MOD = in.nextInt(); int[][] C = new int[n + 1][n + 1]; C[0][0] = 1; for (int i = 1; i < C.length; i++) { C[i][0] = 1; for (int j = 1; j < C.length; j++) { C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; if (C[i][j] >= MOD) { C[i][j] -= MOD; } } } int[] p2 = new int[n + 1]; p2[0] = 1; for (int i = 1; i < p2.length; i++) { p2[i] = 2 * p2[i - 1] % MOD; } int[][] d = new int[n + 1][n + 1]; d[0][0] = 1; // Total. for (int i = 1; i <= n; i++) { // Switched on manually. for (int j = 1; j <= i; j++) { // Switched on manually in the first block. for (int t = 1; t <= j; t++) { if (t == i - 1) { continue; } d[i][j] = (int) ((d[i][j] + (long) C[j][t] * p2[t - 1] % MOD * d[i - t - (i == t ? 0 : 1)][j - t]) % MOD); } } } int ans = 0; for (int k = 1; k <= n; k++) { ans += d[n][k]; if (ans >= MOD) { ans -= MOD; } } out.println(ans); } } static class FastScanner { private BufferedReader in; private StringTokenizer st; public FastScanner(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
cubic
1515_E. Phoenix and Computers
CODEFORCES
//https://github.com/EgorKulikov/yaal/tree/master/lib/main/net/egork import java.util.*; import java.io.*; public class B{ static PrintWriter out; static InputReader in; public static void main(String args[]){ out = new PrintWriter(System.out); in = new InputReader(); new B(); out.flush(); out.close(); } B(){ solve(); } class pair{ int F, S; pair(int a, int b){ F = a; S = b; } } void solve(){ int n = in.nextInt(), mod = in.nextInt(); long dp[][] = new long[n + 1][n + 1]; long ncr[][] = new long[810][410]; ncr[0][0] = 1; for(int i = 1; i < 810; i++){ for(int j = 0; j < 410; j++){ ncr[i][j] = (ncr[i - 1][j] + (j > 0 ? ncr[i - 1][j - 1] : 0)) % mod; } } for(int i = 1; i <= n; i++)dp[i][i] = 1; for(int i = 1; i < n; i++){ for(int j = 1; j + i <= n; j++){ int end = i + j; dp[j][end] = (dp[j + 1][end] + dp[j][end - 1]) % mod; } } long value[] = new long[n + 1]; for(int i = 1; i <= n; i++){ value[i] = dp[1][i]; } long fdp[][] = new long[n + 2][n + 2]; fdp[0][0] = 1; long ans = 0; for(int b = 1; b <= (n + 1) / 2; b++){ for(int i = 1; i <= n; i++){ for(int k = Math.max(0, b - 2); k < i; k++){ fdp[i + 1][b] = (fdp[i + 1][b] + fdp[k][b - 1] * value[i - k] % mod * ncr[k - b + 2 + i - k - 1][i - k] % mod) % mod; } } ans = (ans + fdp[n + 1][b]) % mod; } out.print(ans); } public static class InputReader{ BufferedReader br; StringTokenizer st; InputReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public int nextInt(){ return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } public String next(){ while(st == null || !st.hasMoreTokens()){ try{ st = new StringTokenizer(br.readLine()); }catch(IOException e){} } return st.nextToken(); } } }
cubic
1515_E. Phoenix and Computers
CODEFORCES
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) {new Main().run();} FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); void run(){ work(); out.flush(); } long mod=998244353; long gcd(long a,long b) { return a==0?b:gcd(b%a,a); } void work() { int n=ni(); mod=nl(); long[] dp1=new long[401]; long[][] dp2=new long[n+1][n+1]; long[][] C=new long[401][401]; for(int j=0;j<=400;j++){ for(int i=0;i<=j;i++){ if(i==0||j==i){ C[i][j]=1; }else{ C[i][j]=(C[i-1][j-1]+C[i][j-1])%mod; } } } for(int i=1;i<=400;i++){ for(int j=0;j<i;j++){ dp1[i]=(dp1[i]+C[j][i-1])%mod; } } for(int i=1;i<=n;i++){ dp2[i][i]=dp1[i]; } for(int i=1;i<=n;i++){ for(int j=1;j<i;j++){ for(int k=1;k<j;k++){ dp2[i][j]=dp2[i][j]+((((dp2[i-k-1][j-k]*dp1[k])%mod)*C[k][j])%mod); dp2[i][j]%=mod; } } } long ret=0; for(int j=1;j<=n;j++){ ret=(ret+dp2[n][j])%mod; } out.println(ret); } private ArrayList<long[]>[] ngw(int n, int m) { ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n]; for(int i=0;i<n;i++) { graph[i]=new ArrayList<>(); } for(int i=1;i<=m;i++) { long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong(); graph[(int)s].add(new long[] {e,w}); graph[(int)e].add(new long[] {s,w}); } return graph; } private int ni() { return in.nextInt(); } private long nl() { return in.nextLong(); } private String ns() { return in.next(); } private long[] na(int n) { long[] A=new long[n]; for(int i=0;i<n;i++) { A[i]=in.nextLong(); } return A; } private int[] nia(int n) { int[] A=new int[n]; for(int i=0;i<n;i++) { A[i]=in.nextInt(); } return A; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } public String next() { while(st==null || !st.hasMoreElements())//回车,空行情况 { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
cubic
1515_E. Phoenix and Computers
CODEFORCES
//package com.company.cf.global14; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.List; import java.util.StringTokenizer; import java.util.stream.Collectors; import java.util.stream.IntStream; public class E { FastScanner in; PrintWriter out; private int M; public static void main(String[] args) { new E().solve(); } private void solve() { in = new FastScanner(System.in); out = new PrintWriter(System.out); solveCase(); out.close(); } long[][] comb = new long[512][512]; private void solveCase() { int n = in.nextInt(); this.M = in.nextInt(); calcComb(n, M); for (int i = 0; i < 512; i++) { for (int j = 0; j < 512; j++) { memoDivide[i][j] = -1; memoDP[i][j] = -1; dpG[i] = -1; } } long res = g(n); for (int i = 1;i <= n - 1; i++) { res = (res + dp(n, i)) % M; } out.println(res); } long[] dpG = new long[512]; private long g(int n) { if (n < 2) return 1; if (n == 2) return 2; if (dpG[n] != -1) return dpG[n]; long res = 0; for (int divide = 0; divide < n; divide++) { res = (res + divider(divide, n - divide - 1)) % M; } return (dpG[n] = res); } long[][] memoDivide = new long[512][512]; private long divider(int a, int b) { if (a == 0 || b == 0) return 1; if (memoDivide[a][b] != -1) return memoDivide[a][b]; return (memoDivide[a][b] = ((divider(a - 1, b) + divider(a, b - 1)) % M)); } long[][] memoDP = new long[512][512]; private long dp(int n, int i) { if (n > 0 && i == 0) return 0; if (n == 0 && i > 0) return 0; if (i > n) return 0; if (n == 0 && i == 0) return 1; if (i == n) return g(n); if (i <= 1) return 0; // if (n == 1 && i != 1) return 0; // if (n == 2 && i == 1) return 0; if (memoDP[n][i] != -1) return memoDP[n][i]; // long res = (i * dp(n - 2, i - 1)) % M; long res = 0; for (int failure = 1; failure < n - 1 && i - failure >= 1; failure++) { res += ((g(failure) * comb[i][failure]) % M) * dp(n - failure - 1, i - failure); res %= M; } return (memoDP[n][i] = res); } private void calcComb(int n, int m) { for (int i = 0; i < 512; i++) { comb[0][i] = 0; } for (int i = 0; i < 512; i++) { comb[i][0] = 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % m; } } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
cubic
1515_E. Phoenix and Computers
CODEFORCES
import java.io.*; import java.math.BigInteger; import java.util.*; /** @author KhanhNguyenn */ public class C{ public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } // main solver // sub-problem: 2 orders to merge together x1,x2,..,xn and y1,y2,..,ym // Dựa vào bài toán chia kẹp Euler, ta chứng minh dc số cách merge là // (m+n)C(m-1)=(m+n)C(n+1) static class Task{ int M; public void solve(InputReader in, PrintWriter out) { int n= in.nextInt(); M= in.nextInt(); if(n<=1){ out.println(n); return; } // calculate nCk // nCk= (n-1)C(k-1)+ (n-1)Ck int[][] Ckn= new int[n+1][n+1]; for(int i=0;i<=n;i++){ Ckn[i][i]=1; Ckn[0][i]=1; for(int j=i-1;j>=1;j--){ Ckn[j][i]= add(Ckn[j-1][i-1],Ckn[j][i-1]); } } int ans=0; int[][] dp= new int[n+1][n+1]; dp[1][1]=1; //dp[i][j]: number of ways to turn on first i , using j manually for(int i=2;i<=n;i++){ dp[i][i]= mul(2,dp[i-1][i-1]); for(int j=1;j<=i-1;j++){ for(int k=1;k<=j;k++){ dp[i][j]= add(dp[i][j],mul(mul(dp[k][k],dp[i-k-1][j-k]),Ckn[k][j])); } } } for(int i=0;i<=n;i++) ans= add(ans,dp[n][i]); out.println(ans); } public int add(int a, int b){ a+=b; if(a>=M) a-=M; return a; } public int mul(int a, int b){ long res= (long)a*(long)b; res %=M; return (int)res; } } static class Pair { public String x; public int y; public Pair(String x, int y){ this.x = x; this.y=y; } // @Override // public int compareTo(Pair o) { // if (this.x > o.x){ // return 1; // } // else if (this.x < o.x){ // return -1; // } // else{ // return Integer.compare(this.y, o.y); // } // } } // fast input reader class; static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public String nextToken() { while (st == null || !st.hasMoreTokens()) { String line = null; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble(){ return Double.parseDouble(nextToken()); } public long nextLong(){ return Long.parseLong(nextToken()); } } }
cubic
1515_E. Phoenix and Computers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author lucasr */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; MyScanner in = new MyScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); EPhoenixAndComputers solver = new EPhoenixAndComputers(); solver.solve(1, in, out); out.close(); } static class EPhoenixAndComputers { public static MyScanner sc; public static PrintWriter out; public void solve(int testNumber, MyScanner sc, PrintWriter out) { EPhoenixAndComputers.sc = sc; EPhoenixAndComputers.out = out; int n = sc.nextInt(); long M = sc.nextLong(); MathLib.MOD = M; long[][] comb = MathLib.getComb(n + 5); long[] pots2 = MathLib.getPots(2, n + 2); long[] inv = new long[n + 5]; for (int i = 1; i < inv.length; i++) { inv[i] = MathLib.inverse(i); } long[][] ret = new long[n + 1][n + 1]; // size, cant for (int size = 0; size <= n; size++) { if (size <= 1) { ret[size][size] = 1; } else if (size == 2) { ret[size][2] = 2; } else { long[] me = ret[size]; me[size] = pots2[size - 1]; for (int i = 1; i + 1 < size; i++) { int prev = i, next = size - i - 1; long tmp = pots2[i - 1]; for (int c = 0; c <= next; c++) { long tot = (ret[next][c] * comb[c][c + prev]) % MathLib.MOD; tot = (tot * tmp) % MathLib.MOD; me[prev + c] += tot; me[prev + c] %= MathLib.MOD; } } } } long ans = 0; for (int i = 0; i <= n; i++) { ans += ret[n][i]; } ans %= MathLib.MOD; out.println(ans); } } static class MathLib { public static long MOD = 1000000007; public static long mult(long x, long y) { return (x * y) % MOD; } public static long pow(long base, long exp) { if (exp == 0) return 1; if ((exp & 1) == 1) return mult(base, pow(base, exp - 1)); return pow(mult(base, base), exp / 2); } public static long inverse(long x) { return pow(x, MOD - 2); } public static long[] getPots(long base, int max) { long[] ret = new long[max + 1]; ret[0] = 1; for (int i = 1; i <= max; i++) { ret[i] = mult(ret[i - 1], base); } return ret; } public static long[][] getComb(int max) { long[][] ret = new long[max + 1][max + 1]; for (int n = 0; n <= max; n++) { ret[0][n] = ret[n][n] = 1; for (int k = 1; k < n; k++) { ret[k][n] = (ret[k - 1][n - 1] + ret[k][n - 1]) % MOD; } } return ret; } } static class MyScanner { private BufferedReader br; private StringTokenizer tokenizer; public MyScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
cubic
1515_E. Phoenix and Computers
CODEFORCES
//make sure to make new file! import java.io.*; import java.util.*; public class EG14{ public static long MOD; public static int MAX = 405; public static long[] fac; public static long[] ifac; public static void main(String[] args)throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); MOD = Long.parseLong(st.nextToken()); long[] pow2 = new long[MAX]; pow2[0] = 1L; for(int k = 1; k < MAX; k++){ pow2[k] = (2L*pow2[k-1] + MOD)%MOD; } fac = new long[MAX]; ifac = new long[MAX]; fac[0] = 1L; ifac[0] = 1L; for(int k = 1; k < MAX; k++){ fac[k] = ((long)k*fac[k-1] + MOD)%MOD; ifac[k] = modInverse(fac[k],MOD); } long[][] dp = new long[n][n+1]; //what n you're on, what how many computers you've turned on manually //initial for(int k = 0; k < n; k++){ dp[k][k+1] = pow2[k]; } for(int k = 2; k < n; k++){ for(int j = 1; j <= n; j++){ if(dp[k-2][j-1] == 0) continue; long start = dp[k-2][j-1]; //number for part up to previous block for(int add = 1; ; add++){ if(k+add-1 >= n || j+add-1 > n) break; long adder = (start * pow2[add-1] + MOD)%MOD; adder = (adder * choose(j+add-1,j-1) + MOD)%MOD; dp[k+add-1][j+add-1] = (dp[k+add-1][j+add-1] + adder + MOD)%MOD; } } } long answer = 0L; for(int k = 1; k <= n; k++){ answer = (answer + dp[n-1][k] + MOD)%MOD; } out.println(answer); out.close(); } //a choose b public static long choose(int a, int b){ long prod = (fac[a]*ifac[b] + MOD)%MOD; return (prod*ifac[a-b] + MOD)%MOD; } //from geeksforgeeks public static long modInverse(long a, long m) { long m0 = m; long y = 0L; long x = 1L; if (m == 1L) return 0L; while (a > 1L) { // q is quotient long q = a / m; long t = m; // m is remainder now, process same as // Euclid's algo m = a % m; a = t; t = y; // Update y and x y = x - q * y; x = t; } // Make x positive if (x < 0L) x += m0; return x; } }
cubic
1515_E. Phoenix and Computers
CODEFORCES
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; import java.util.List; import java.util.*; public class realfast implements Runnable { private static final int INF = (int) 1e9; long in= 1000000007; long fac[]= new long[1000001]; long inv[]=new long[1000001]; public void solve() throws IOException { //int t = readInt(); int n = readInt(); long m = readInt(); long method[][]=new long [n+1][n+1]; for(int i=0;i<=n;i++) { method[0][i]=1; method[i][0]=1; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { for(int k=i;k>=0;k--) { method[i][j]= (method[i][j]%m+method[k][j-1]%m)%m; } } } // index , total , cont //long dp[][][]=new long[n+2][n+2][n+2]; long sum[][]=new long[n+2][n+2]; sum[0][0]=1; // dp[0][0][0]=1; long len[][]=new long[n+1][n+1]; for(int i=0;i<=n;i++) { len[i][0]=1; len[0][i]=1; } for(int i=2;i<=n;i++) { for(int j=1;j<i;j++) { len[j][i-j]= (len[j-1][i-j]%m+len[j][i-j-1]%m)%m; } } long gal[]=new long[2*n+1]; for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++) { gal[i+j]= (gal[i+j]+ len[i][j])%m; } } for(int i=1;i<=n;i++) { if(i==n-1) continue; for(int j=1;j<=i;j++) { for(int k=1;k<=j;k++) { long val =sum[i-k][j-k]; val = (val*method[j-k][k])%m; val =(val*gal[k-1])%m; sum[i+1][j]= (sum[i+1][j]+val)%m; } } } long ans =0; for(int i=1;i<=n;i++) { ans = (ans + sum[n+1][i])%m; } out.println(ans); } public int value (int seg[], int left , int right ,int index, int l, int r) { if(left>right) { return -100000000; } if(right<l||left>r) return -100000000; if(left>=l&&right<=r) return seg[index]; int mid = left+(right-left)/2; int val = value(seg,left,mid,2*index+1,l,r); int val2 = value(seg,mid+1,right,2*index+2,l,r); return Math.max(val,val2); } public int gcd(int a , int b ) { if(a<b) { int t =a; a=b; b=t; } if(a%b==0) return b ; return gcd(b,a%b); } public long pow(long n , long p,long m) { if(p==0) return 1; long val = pow(n,p/2,m);; val= (val*val)%m; if(p%2==0) return val; else return (val*n)%m; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { new Thread(null, new realfast(), "", 128 * (1L << 20)).start(); } private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; private BufferedReader reader; private StringTokenizer tokenizer; private PrintWriter out; @Override public void run() { try { if (ONLINE_JUDGE || !new File("input.txt").exists()) { reader = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { reader = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } solve(); } catch (IOException e) { throw new RuntimeException(e); } finally { try { reader.close(); } catch (IOException e) { // nothing } out.close(); } } private String readString() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } @SuppressWarnings("unused") private int readInt() throws IOException { return Integer.parseInt(readString()); } @SuppressWarnings("unused") private long readLong() throws IOException { return Long.parseLong(readString()); } @SuppressWarnings("unused") private double readDouble() throws IOException { return Double.parseDouble(readString()); } } class edge implements Comparable<edge>{ int u ; int v; edge(int u, int v) { this.u=u; this.v=v; } public int compareTo(edge e) { return this.v-e.v; } }
cubic
1515_E. Phoenix and Computers
CODEFORCES
import java.io.*; import java.text.DecimalFormat; import java.util.*; public class Main { public static final DecimalFormat DF_3 = new DecimalFormat("0.000"); private static final long MOD = 1000000007; static int[] readArray(int size, InputReader in, boolean subOne) { int[] a = new int[size]; for (int i = 0; i < size; i++) { a[i] = in.nextInt() + (subOne ? -1 : 0); } return a; } static long[] readLongArray(int size, InputReader in) { long[] a = new long[size]; for (int i = 0; i < size; i++) { a[i] = in.nextLong(); } return a; } static void sortArray(int[] a) { Random random = new Random(); for (int i = 0; i < a.length; i++) { int randomPos = random.nextInt(a.length); int t = a[i]; a[i] = a[randomPos]; a[randomPos] = t; } Arrays.sort(a); } private static long[] allInvs(int n, long mod) { long[] inv = new long[n + 1]; inv[1] = 1; for (int i = 2; i <= n; ++i) { inv[i] = subMod(mod, (mod / i) * inv[(int) (mod % i)] % mod, mod); } return inv; } private static long subMod(long x, long y, long mod) { long res = x - y; if (res < 0) { return res + mod; } return res; } private static long fastPow(long x, long y, long mod) { if (x == 1) { return 1; } if (y == 0) { return 1; } long p = fastPow(x, y / 2, mod) % mod; p = p * p % mod; if (y % 2 == 1) { p = p * x % mod; } return p; } public static void main(String[] args) throws FileNotFoundException { // InputReader in = new InputReader(new FileInputStream("input.txt")); // PrintWriter out = new PrintWriter(new BufferedOutputStream(new FileOutputStream("milkvisits.out"))); // InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); // long start = System.currentTimeMillis(); int n = in.nextInt(); long mod = in.nextLong(); long[] invs = allInvs(n + 3, mod); long[] facts = new long[n + 2]; facts[0] = 1; long[] invFacts = new long[n + 2]; invFacts[0] = 1; for (int i = 1; i < n + 2; i++) { facts[i] = (facts[i - 1] * i) % mod; invFacts[i] = (invFacts[i - 1] * invs[i]) % mod; } long[] pow2 = new long[n+3]; pow2[0] = 1; for (int i = 1; i < n+3; i++) { pow2[i] = pow2[i-1] * 2 % mod; } long[][] dp = new long[n + 2][n + 2]; for (int i = 2; i <= n + 1; i++) { dp[i][1] = invFacts[i - 1] * pow2[i - 2] % mod; for (int k = 2; k <= n; k++) { for (int j = i - 2; j >= 1; j--) { dp[i][k] = (dp[i][k] + dp[j][k - 1] * pow2[ i - j - 2] % mod * invFacts[i - j - 1] % mod) % mod; } } } long ans = 0; for (int k = 1; k <= n; k++) { ans = (ans + dp[n + 1][k] * facts[n - k + 1] % mod) % mod; } out.println(ans); out.close(); } private static void outputArray(List<Long> ans, PrintWriter out, boolean addOne) { StringBuilder str = new StringBuilder(); for (int j = 0; j < ans.size(); j++) { long i = ans.get(j); long an = i + (addOne ? 1 : 0); str.append(an); if (j < ans.size() - 1) { str.append(' '); } } out.println(str); // out.flush(); } private static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextString() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public char nextChar() { return next().charAt(0); } public String nextWord() { return next(); } private List<Integer>[] readTree(int n) { return readGraph(n, n - 1); } private List<Integer>[] readGraph(int n, int m) { List<Integer>[] result = new ArrayList[n]; for (int i = 0; i < n; i++) { result[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; result[u].add(v); result[v].add(u); } return result; } } }
cubic
1515_E. Phoenix and Computers
CODEFORCES