Bestcoder #49

来源:互联网 发布:javascript generator 编辑:程序博客网 时间:2024/04/29 23:15

1001 Untitled

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)

/** * 2015年8月1日 下午7:15:14 * PrjName:Bc49-01 * @ Semprathlon */import java.io.*;import java.util.*;public class Main {    static ArrayList<Integer> v=new ArrayList<Integer>();    static int[] a,f;    static int lowbit(int x){        return x&(-x);    }    static int get(int x){        int res=0;        while(x>0){            res++;            x-=lowbit(x);        }        return res;    }    public static void main(String[] args) throws IOException{        // TODO Auto-generated method stub        InputReader in=new InputReader(System.in);        PrintWriter out=new PrintWriter(System.out);        int T=in.nextInt();        while(T-->0){            v.clear();            int n=in.nextInt();            int m=in.nextInt();            for(int i=1;i<=n;i++)                v.add(in.nextInt());            Collections.sort(v);            f=new int[1<<n];            Arrays.fill(f, -1);            f[0]=m;            //out.println(n);            int ans=Integer.MAX_VALUE;            for(int i=0;i<(1<<n);i++)                if (f[i]>0)                    for(int j=0;j<n;j++)                        if (((1<<j)&i)==0){                            int k=i|(1<<j);                            f[k]=f[i]%v.get(j);                            if (f[k]==0)                                ans=Math.min(ans, get(k));                        }            if (ans==Integer.MAX_VALUE) out.println(-1);            else out.println(ans);        }        out.flush();        out.close();    }}

1002 Three Palindromes

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)

  • Problem Description
    Can we divided a given string S into three nonempty palindromes?

  • Input
    First line contains a single integer latexT20 which denotes the number of test cases.
    For each test case , there is an single line contains a string S which only consist of lowercase English letters.latex1|s|20000

  • Output
    For each case, output the “Yes” or “No” in a single line.

  • Sample Input
    2
    abc
    abaadada

  • Sample Output
    Yes
    No

赛时的Hash做法:

/** * 2015年8月1日 下午7:47:15 * PrjName:Bc49-02 * @ Semprathlon */import java.io.*;import java.util.*;public class Main {    static long pri=29L;    static long lh(String s,long res,int p){        return res*pri+(long)(s.charAt(p)-'a');    }    static long rh(String s,long res,int p,long m){        return res+(long)(s.charAt(p)-'a')*m;    }    static BitSet v=new BitSet(20010);    public static void main(String[] args) throws IOException{        // TODO Auto-generated method stub        InputReader in=new InputReader(System.in);        PrintWriter out=new PrintWriter(System.out);        int T=in.nextInt();        while(T-->0){            String s=new String(in.next());            int len=s.length();            //s=s+"#"+(new StringBuffer(s).reverse().toString());            //out.println(s);            boolean ans=false;            long l1=0L,r1=0L,tmp=1L;            for(int i=0;i<len;i++){                if (ans) break;                l1=lh(s, l1, i);                r1=rh(s, r1, i, tmp);                //out.println(i+" "+l1+" "+r1);                tmp*=pri;                //if ((i&1)>0) continue;                if (l1==r1){                    //out.println("f"+i);                    long l2=0L,r2=0L,tmp2=1L;                    v.clear();                    for(int j=i+1;j<len;j++){                        l2=lh(s,l2,j);                        r2=rh(s,r2,j,tmp2);                        tmp2*=pri;                        //if (((j-i+1)&1)>0) continue;                        if (l2==r2) v.set(j);                    }                    l2=0L;r2=0L;tmp2=1L;                    for(int j=len-1;j>i;j--){                        l2=lh(s,l2,j);                        r2=rh(s,r2,j,tmp2);                        tmp2*=pri;                        //if (((len-j+1)&1)>0) continue;                        if (l2==r2&&v.get(j-1)){                            ans=true;break;                        }                    }                }            }            out.println(ans?"Yes":"No");        }        out.flush();        out.close();    }}

首次尝试去hack别人不成,还被人黑了,立马TLE

题解介绍的多种方法中,个人认为二分+hash代码量较少,如何实现?

0 0
原创粉丝点击