usaco 4.1 Cryptcowgraphy

来源:互联网 发布:公司如何激励员工 知乎 编辑:程序博客网 时间:2024/05/21 14:04
/*ID: daniel.20LANG: JAVATASK: cryptcow*/import java.io.*;import java.math.BigInteger;import java.util.*;class problem{    String target = "Begin the Escape execution at the Break of Dawn";    StringBuilder sb = new StringBuilder();    boolean hash_arr[];    //int big_primer = 502973;    int big_primer = 899913;    boolean flag = false;    int ELFhash(String str){        long hash1 = 0, g=0;        for(int i=0;i<str.length();i++){            hash1=(hash1<<4) + str.charAt(i);            g=hash1&0xF0000000L;            if(g!=0) hash1^=g>>24;            hash1&=~g;        }        return (int)((hash1%big_primer+big_primer)%big_primer);    }    int BKDRhash(String str){        int seed = 131;        long hash1=0;        for(int i=0;i<str.length();i++){            hash1+=hash1*seed+str.charAt(i);            hash1= (hash1%big_primer + big_primer)%big_primer;        }        return (int)hash1;    }    void solver() throws IOException{        //BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));        BufferedReader reader = new BufferedReader(new FileReader("cryptcow.in"));        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("cryptcow.out")));        String t = reader.readLine();        hash_arr = new boolean[big_primer];        dfs(t,0);        if(!flag) sb.append("0 0").append("\n");        System.out.print(sb.toString());        out.print(sb.toString());        out.close();        System.exit(0);    }    void dfs(String t, int level){        if(flag) return;        if(t.equals(target)){            sb.append("1 ").append(level).append("\n");            flag = true;            return;        }        int pos_c[] = new int[10];        int pos_o[] = new int[10];        int pos_w[] = new int[10];        int c1=0, c2=0, c3=0;        for(int i=0;i<t.length();i++){            if(t.charAt(i)=='C') pos_c[c1++]=i;            if(t.charAt(i)=='O') pos_o[c2++]=i;            if(t.charAt(i)=='W') pos_w[c3++]=i;        }        //cut1        if(c1>c2||c1>c3||c2>c3){            sb.append("0 0").append("\n");            return;        }        //cut1        for(int i=0;i<t.length();i++){            if(t.charAt(i)=='C') break;            if(t.charAt(i)=='W'||t.charAt(i)=='O') return;            if(t.charAt(i)!=target.charAt(i)){                return;            }        }        //cut2        for(int i=t.length()-1, j=target.length()-1;i>=0&&j>=0;i--,j--){            if(t.charAt(i)=='W') break;            if(t.charAt(i)=='C'||t.charAt(i)=='O') return;            if(t.charAt(i)!=target.charAt(j)){                return;            }        }             //cut3        int last_pos = -1;        for(int i=0;i<t.length();i++){            if(t.charAt(i)=='W'||t.charAt(i)=='C'||t.charAt(i)=='O'){                String tmp = t.substring(last_pos+1,i);                if(tmp.length()>0&&!target.contains(tmp)){                    return;                }                last_pos=i;            }        }        for(int i=0;i<c1;i++){            for(int j=0;j<c2;j++){                if(pos_o[j]<=pos_c[i]) continue;                for(int k=0;k<c3;k++){                    if(pos_w[k]<pos_o[j]||pos_w[k]<pos_c[i]) continue;                    String new_string = t.substring(0,pos_c[i])+t.substring(pos_o[j]+1,pos_w[k])+t.substring(pos_c[i]+1,pos_o[j])+t.substring(pos_w[k]+1);                    int hash_v = ELFhash(new_string);                    if(hash_arr[hash_v])continue;                    hash_arr[hash_v] = true;                    dfs(new_string,level+1);                }            }        }    }}public class cryptcow {    public static void main (String [] args) throws Exception {        problem p = new problem();        p.solver();    }}

 

先留个字符串hash函数的link, 高手的博客

https://www.byvoid.com/blog/string-hash-compare


这题A得一头雾水,裸的代码是跑不过去T6的

然后想不出怎么剪,看了官网后加了3个剪枝,cut2 - cut4, 这3个都很重要

 

然后还是WA T8

感觉最后纯粹是靠着ELFhash的冲突过去了,相同的参数如果换BKDRhash就超时了,完全是冲突没处理而少了很多搜索

感觉A得纯粹靠调参数,参数换成1000013就又超时了一点,所以A得一头雾水=.=

0 0
原创粉丝点击