usaco/ 1.2 milk2/transform/namenum/palsquare/dualpal

/*
ID: chicc991
LANG: JAVA
TASK: milk2
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.BitSet;
import java.util.Scanner;


public class milk2 {
	public static void main(String[] args) throws Exception
	{
		Scanner sc =new Scanner(new FileReader(new File("data/milk2.in")));
		
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("data/milk2.out")));
			int n=sc.nextInt();
			BitSet time=new BitSet(1000000);
			for(int i=0;i<n;i++)
			{
				int start=sc.nextInt();
				int end=sc.nextInt();
				for(int j=start;j<end;j++)
					time.set(j);
					
			}
			
			int maxtime=0;
			int maxtime2=0;
			int k=0;
			int m=0;
			for(int i=0;i<1000000;i++)
			{
				if(time.get(i))
				{
					k++;
					
					if(m>maxtime2)
						maxtime2=m;
					m=0;
					
				}else{
					if(k>maxtime)
						maxtime=k;
					k=0;
					if(maxtime!=0)
						m++;
				}
			}
			
			out.println(maxtime+" "+maxtime2);
				
		out.close();
		System.exit(0);
	}
}


利用比特位,如果有奶农挤奶,则置为true


package chapter1_2;

/*
 ID: chicc991
 LANG: JAVA
 TASK: transform
 */
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class transform {
	private int[][] a;
	private int[][] s;
	private int N;

	public static void main(String[] args) throws Exception {
		transform main = new transform();
	}

	public transform() throws Exception {
		Scanner sc = new Scanner(new FileReader(new File("data/transform.in")));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				"data/transform.out")));
		while (sc.hasNext()) {
			N = sc.nextInt();
			a = new int[N][N];
			s = new int[N][N];
			for (int i = 0; i < N; i++) {
				char[] c = sc.next().toCharArray();
				for (int j = 0; j < N; j++) {
					if (c[j] == '@')
						a[i][j] = 0;
					else
						a[i][j] = 1;
				}
			}

			for (int i = 0; i < N; i++) {
				char[] c = sc.next().toCharArray();
				for (int j = 0; j < N; j++) {
					if (c[j] == '@')
						s[i][j] = 0;
					else
						s[i][j] = 1;
				}
			}

			out.println(transform(s, a));

		}
		sc.close();
		out.close();
	}

	private int transform(int[][] a, int[][] b) {
		if (isSame(a, rotate90(b)))
			return 1;
		else if (isSame(a, rotate180(b)))
			return 2;
		else if (isSame(a, rotate270(b)))
			return 3;
		else if (isSame(a, reflect(b)))
			return 4;
		else if (isSame(a, combination90(b)) || isSame(a, combination180(b))
				|| isSame(a, combination270(b)))
			return 5;
		else if (isSame(a, b))
			return 6;
		else
			return 7;

	}

	private boolean isSame(int[][] a, int[][] b) {

		for (int i = 0; i < N; i++)
			for (int j = 0; j < N; j++) {
				if (a[i][j] != b[i][j])
					return false;
			}
		return true;
	}

	private int[][] rotate90(int[][] a) {
		int[][] b = new int[N][N];
		for (int i = 0; i < N; i++)
			for (int j = 0; j < N; j++) {
				b[i][j] = a[N - 1 - j][i];
			}

		return b;

	}

	private int[][] rotate180(int[][] a) {
		return rotate90(rotate90(a));
	}

	private int[][] rotate270(int[][] a) {
		return rotate90(rotate90(rotate90(a)));
	}

	private int[][] reflect(int[][] a) {
		int[][] b = new int[N][N];
		for (int i = 0; i < N; i++)
			for (int j = 0; j < N; j++) {
				b[i][j] = a[i][N - 1 - j];
			}
		return b;
	}

	private int[][] combination90(int[][] a) {
		return rotate90(reflect(a));
	}

	private int[][] combination180(int[][] a) {
		return rotate180(reflect(a));
	}

	private int[][] combination270(int[][] a) {
		return rotate270(reflect(a));
	}

}

注意矩阵旋转90°的实现


namenum

package chapter1_2;
/*
ID: chicc991
LANG: JAVA
TASK: namenum
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;


public class namenum
{

	private HashSet<String> dict;
	private HashMap<String, String> map;
	private char[] c;

	private StringBuilder sb;
	private boolean flag;
	private PrintWriter out;

	public static void main(String[] args) throws Exception
	{
		namenum main = new namenum();

	}

	public namenum() throws Exception
	{
		init();
		foo();
	}

	private void init() throws Exception
	{
		Scanner sc = new Scanner(new FileReader(new File("data/dict.txt")));
		dict = new HashSet<String>();
		while (sc.hasNext())
			dict.add(sc.next());
		map=new HashMap<String,String>();
		map.put("2", "ABC");
		map.put("3", "DEF");
		map.put("4", "GHI");
		map.put("5", "JKL");
		map.put("6", "MNO");
		map.put("7", "PRS");
		map.put("8", "TUV");
		map.put("9", "WXY");
		
	}

	private void foo() throws Exception
	{

		Scanner sc = new Scanner(new FileReader(new File("data/namenum.in")));
		out = new PrintWriter(new BufferedWriter(new FileWriter(
				"data/namenum.out")));
		
		while(sc.hasNext())
		{
			flag=false;
			sb=new StringBuilder();
			c=sc.next().toCharArray();
			too("",0);
			
			if(!flag)
				out.println("NONE");
			else
				out.print(sb);
		}
		out.close();
		sc.close();
	}
	
	private void too(String s,int i)
	{

		if(i==c.length)
		{
			if(dict.contains(s))
			{
				sb.append(s+"\n");
				flag=true;
			}
			return;
				
		}
		String str=map.get(""+c[i]);
		too(s+str.charAt(0),i+1);
		too(s+str.charAt(1),i+1);
		too(s+str.charAt(2),i+1);
	}
}

将每个输入映射成对应的字符串,并对比是否存在文件中。第十个case超时。

编号最多为12位数字,最多有3^12种可能,当文件中的字符串存储在哈希表中时,时间复杂度O(3^n)*O(1)。

考虑文件中的字符串仅仅不到5000,先将其转为数字字符串,时间复杂度O(M*N),其中M为文件中字符串个数小于5000.

以这些字符串转换后的值为key,原值为value存进hashmap。考虑到一串数字字符串可以对应多个值,所以键值对中的值使用集合。因为输出要有序,考虑treeset。

代码如下,最坏情况下时间比上面方法节省60%

package chapter1_2;

/*
 ID: chicc991
 LANG: JAVA
 TASK: namenum
 */
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeSet;

public class namenum
{

	private HashMap<String, TreeSet<String>> dict;
	private HashMap<String, String> map;

	public static void main(String[] args) throws Exception
	{
		namenum main = new namenum();

	}

	public namenum() throws Exception
	{
		init();
		too();

	}

	private void init() throws Exception
	{
		Scanner sc = new Scanner(new FileReader(new File("data/dict.txt")));
		dict = new HashMap<String, TreeSet<String>>();

		map = new HashMap<String, String>();
		map.put("A", "2");
		map.put("B", "2");
		map.put("C", "2");
		map.put("D", "3");
		map.put("E", "3");
		map.put("F", "3");
		map.put("G", "4");
		map.put("H", "4");
		map.put("I", "4");
		map.put("J", "5");
		map.put("K", "5");
		map.put("L", "5");
		map.put("M", "6");
		map.put("N", "6");
		map.put("O", "6");
		map.put("P", "7");
		map.put("R", "7");
		map.put("S", "7");
		map.put("T", "8");
		map.put("U", "8");
		map.put("V", "8");
		map.put("W", "9");
		map.put("X", "9");
		map.put("Y", "9");

		while (sc.hasNext()) {
			String strValue = sc.next();
			String strKey = foo(strValue.toCharArray());
			TreeSet<String> arr;
			if (dict.containsKey(strKey))

				arr = dict.get(strKey);

			else

				arr = new TreeSet<String>();

			arr.add(strValue);
			dict.put(strKey, arr);

		}

	}

	private String foo(char[] c)
	{
		String s = "";
		for (int i = 0; i < c.length; i++)
			s += map.get(c[i] + "");
		return s;

	}

	private void too() throws Exception
	{
		Scanner sc = new Scanner(new FileReader(new File("data/namenum.in")));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				"data/namenum.out")));

		while (sc.hasNext()) {
			String str = sc.next();
			if (dict.containsKey(str)) {
				TreeSet<String> arr = dict.get(str);
				while (!arr.isEmpty())
					out.println(arr.pollFirst());

			} else
				out.println("NONE");

		}
		out.close();
		sc.close();
	}

}


palsquare

package chapter1_2;

/*
 ID: chicc991
 LANG: JAVA
 TASK: palsquare
 */
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class palsquare
{
	private PrintWriter out;

	public static void main(String[] args) throws Exception
	{
		palsquare main = new palsquare();
	}

	public palsquare() throws Exception
	{
		Scanner sc = new Scanner(new FileReader(new File("data/palsquare.in")));
		out = new PrintWriter(new BufferedWriter(new FileWriter(
				"data/palsquare.out")));
		while (sc.hasNext()) {
			int b = sc.nextInt();
			for (int i = 1; i <= 300; i++) {
				Palindrome(i, b);
			}

		}
		out.close();
		sc.close();
	}

	private void Palindrome(int a, int b)
	{
		String s = change(a * a, b);
		if (isPalindrome(s)) {
			out.println(change(a, b) + " " + s);
		}

	}

	private String change(int a, int b)
	{
		String str = "";

		int r;
		while (true) {
			if (a == 0)
				break;
			else {
				r = a % b;
				a = a / b;

				str = foo(r, b) + str;
			}
		}
		return str;
	}

	private String foo(int r, int b)
	{
		if (r < 10)
			return r + "";
		else {
			char a = (char) (r - 10 + 'A');
			return a + "";
		}
	}

	private boolean isPalindrome(String s)
	{
		char[] c = s.toCharArray();
		int i = 0;
		int j = s.length() - 1;
		while (i <= j) {
			if (c[i++] != c[j--])
				return false;
		}
		return true;
	}
}

简单题,注意
char a = (char) (r - 10 + 'A');

如果直接输出r-10+'A',则输出时对应的ascii码


dualpal

package chapter1_2;

/*
ID: chicc991
LANG: JAVA
TASK: dualpal
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class dualpal
{
	private Scanner sc;
	private PrintWriter out;

	public static void main(String[] args) throws Exception
	{
		dualpal main = new dualpal();

	}

	public dualpal() throws Exception
	{
		sc = new Scanner(new FileReader(new File("data/dualpal.in")));
		out = new PrintWriter(new BufferedWriter(new FileWriter(
				"data/dualpal.out")));
		while (sc.hasNext()) {
			int n = sc.nextInt();
			int s = sc.nextInt();
			foo(n,s);
		}
		out.close();
		sc.close();
	}

	private void foo(int n, int s)
	{
		int i=s+1;
		while(n>0)
		{
			if(isDualpal(i))
			{
				n--;
				out.println(i);
			}
			i++;
		}
	}
	
	//判断i是否在至少2种进制中时回文数
	private boolean isDualpal(int i)
	{
		int flag=0;
		for(int j=2;j<=10;j++)
		{
			if(isPalindrome(change(i,j)))
				flag++;
			if(flag==2)
				return true;
		}
		return false;
	}
	
	//判断是否是回文数
	private boolean isPalindrome(String str)
	{
		char[] c=str.toCharArray();
		if(c[0]=='0')
			return false;
		int i=0;
		int j=c.length-1;
		while(i<=j)
		{
			if(c[i++]!=c[j--])
				return false;
		}
		return true;
	}

	//将10进制s转化为b进制,返回对应字符串
	private String change(int s, int b)
	{
		String str = "";
		int r;
		while (true) {
			if (s == 0)

				break;

			r = s % b;
			s = s / b;

			str = r + str;
		}
		return str;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值