Submission #337888


Source Code Expand

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main {
	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";
	
	static void solve()
	{
		int n = ni();
		char[][] s = new char[n][];
		for(int i = 0;i < n;i++){
			s[i] = ns().toCharArray();
		}
		
		int minlen = 9999999;
		for(int i = 0;i < n;i++){
			minlen = Math.min(minlen, s[i].length);
		}
		int[] row = new int[n];
		for(int i = 0;i < minlen;i++){
			inner:
			for(int j = 0;j < 26;j++){
				int[][] g = new int[n][];
				for(int k = 0;k < n;k++){
					int p = 0;
					for(int l = 0;l < n;l++){
						if(l+i < s[k].length && s[k][l+i] == 'a'+j){
							row[p++] = l;
						}
					}
					if(p == 0)continue inner;
					g[k] = Arrays.copyOf(row, p);
				}
				int M = doBipartiteMatchingHK(g, n);
				if(M == n){
					out.println("YES");
					return;
				}
			}
		}
		out.println("NO");
	}
	
	public static int doBipartiteMatchingHK(int[][] g, int m)
	{
		int n = g.length;
		if(n == 0)return 0;
		int[] from = new int[m];
		int[] to = new int[n];
		Arrays.fill(to, -1);
		Arrays.fill(from, n);
		
		int[] d = new int[n+1];
		int mat = 0;
		while(true){
			Arrays.fill(d, -1);
			int[] q = new int[n];
			int r = 0;
			for(int i = 0;i < n;i++){
				if(to[i] == -1){
					d[i] = 0;
					q[r++] = i;
				}
			}
			
			for(int p = 0;p < r;p++) {
				int cur = q[p];
				for(int adj : g[cur]){
					int nex = from[adj];
					if(d[nex] == -1) {
						if(nex != n)q[r++] = nex;
						d[nex] = d[cur] + 1;
					}
				}
			}
			if(d[n] == -1)break;
			
			for(int i = 0;i < n;i++){
				if(to[i] == -1){
					if(dfsHK(d, g, n, m, to, from, i))mat++;
				}
			}
		}
		return mat;
	}
	
	static boolean dfsHK(int[] d, int[][] g, int n, int m, int[] to, int[] from, int cur)
	{
		if(cur == n)return true;
		for(int adj : g[cur]){
			int nex = from[adj];
			if(d[nex] == d[cur] + 1 && dfsHK(d, g, n, m, to, from, nex)){
				to[cur] = adj;
				from[adj] = cur;
				return true;
			}
		}
		d[cur] = -1;
		return false;
	}

	
	public static void main(String[] args) throws Exception
	{
		long S = System.currentTimeMillis();
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		solve();
		out.flush();
		long G = System.currentTimeMillis();
		tr(G-S+"ms");
	}
	
	private static boolean eof()
	{
		if(lenbuf == -1)return true;
		int lptr = ptrbuf;
		while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
		
		try {
			is.mark(1000);
			while(true){
				int b = is.read();
				if(b == -1){
					is.reset();
					return true;
				}else if(!isSpaceChar(b)){
					is.reset();
					return false;
				}
			}
		} catch (IOException e) {
			return true;
		}
	}
	
	private static byte[] inbuf = new byte[1024];
	static int lenbuf = 0, ptrbuf = 0;
	
	private static int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
//	private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); }
	private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private static double nd() { return Double.parseDouble(ns()); }
	private static char nc() { return (char)skip(); }
	
	private static String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private static char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private static char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private static int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private static int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}

Submission Info

Submission Time
Task B - コメント
User uwi
Language Java (OpenJDK 1.7.0)
Score 30
Code Size 5339 Byte
Status TLE
Exec Time 2040 ms
Memory 31700 KB

Judge Result

Set Name Sample Subtask1 Subtask2
Score / Max Score 0 / 0 30 / 30 0 / 60
Status
AC × 2
AC × 18
AC × 36
TLE × 8
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
Subtask1 sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt
Subtask2 subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask2_09.txt, subtask2_10.txt, subtask2_11.txt, subtask2_12.txt, subtask2_13.txt, subtask2_14.txt, subtask2_15.txt, subtask2_16.txt, subtask2_17.txt, subtask2_18.txt, subtask2_19.txt, subtask2_20.txt, subtask2_21.txt, subtask2_22.txt, subtask2_23.txt, subtask2_24.txt, subtask2_25.txt, subtask2_26.txt, subtask2_27.txt, subtask2_28.txt
Case Name Status Exec Time Memory
sample_01.txt AC 429 ms 20884 KB
sample_02.txt AC 418 ms 20600 KB
subtask1_01.txt AC 431 ms 20680 KB
subtask1_02.txt AC 456 ms 21776 KB
subtask1_03.txt AC 442 ms 22068 KB
subtask1_04.txt AC 442 ms 22160 KB
subtask1_05.txt AC 436 ms 20648 KB
subtask1_06.txt AC 534 ms 26996 KB
subtask1_07.txt AC 431 ms 20712 KB
subtask1_08.txt AC 422 ms 20656 KB
subtask1_09.txt AC 422 ms 20660 KB
subtask1_10.txt AC 451 ms 21996 KB
subtask1_11.txt AC 435 ms 22108 KB
subtask1_12.txt AC 466 ms 25140 KB
subtask1_13.txt AC 418 ms 20708 KB
subtask1_14.txt AC 445 ms 21736 KB
subtask1_15.txt AC 440 ms 21392 KB
subtask1_16.txt AC 430 ms 22048 KB
subtask2_01.txt AC 531 ms 27280 KB
subtask2_02.txt AC 1212 ms 30728 KB
subtask2_03.txt AC 452 ms 23164 KB
subtask2_04.txt TLE 2038 ms 31132 KB
subtask2_05.txt AC 460 ms 22712 KB
subtask2_06.txt AC 469 ms 22852 KB
subtask2_07.txt AC 464 ms 22740 KB
subtask2_08.txt AC 471 ms 22928 KB
subtask2_09.txt AC 1191 ms 31072 KB
subtask2_10.txt AC 1295 ms 31236 KB
subtask2_11.txt AC 487 ms 24160 KB
subtask2_12.txt AC 1114 ms 30800 KB
subtask2_13.txt AC 928 ms 31028 KB
subtask2_14.txt AC 1261 ms 30948 KB
subtask2_15.txt AC 837 ms 30900 KB
subtask2_16.txt AC 654 ms 31116 KB
subtask2_17.txt AC 542 ms 30700 KB
subtask2_18.txt AC 645 ms 30532 KB
subtask2_19.txt AC 625 ms 30512 KB
subtask2_20.txt TLE 2040 ms 31368 KB
subtask2_21.txt TLE 2024 ms 30984 KB
subtask2_22.txt TLE 2012 ms 31408 KB
subtask2_23.txt TLE 2038 ms 31432 KB
subtask2_24.txt TLE 2040 ms 31568 KB
subtask2_25.txt AC 1996 ms 30796 KB
subtask2_26.txt AC 1163 ms 31300 KB
subtask2_27.txt TLE 2040 ms 31700 KB
subtask2_28.txt TLE 2038 ms 31308 KB