您的位置:首页 > 其它

POJ3349 HASH

2017-02-17 15:41 435 查看
给你n个六个数的序列,每一个序列代表一朵雪花,问这n个雪花中存不存相同的雪花,

若存在输出Twin snowflakes found.,否则输出No
two snowflakes are alike.

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
public static void main(String args[]) {
new Task().solve() ;
}
}

class Task{
InputReader in = new InputReader(System.in) ;
PrintWriter out = new PrintWriter(System.out) ;

final int N = 100008 ;
final int Mod = 100007 ;
class Node{
int[] v ;
int next ;
Node(){
v = new int[6] ;
}
}
Node[] node = new Node[N<<1] ;
int[] head = new int[Mod] ;
int idx ;

void add(int u , int[] v){
node[idx] = new Node() ;
System.arraycopy(v , 0 , node[idx].v , 0 , 6) ;
node[idx].next = head[u] ;
head[u] = idx++ ;
}

boolean find(int u , int[] v){
for(int i = head[u] ; i != -1 ; i = node[i].next){
Node now = node[i] ;
boolean ex = true ;
for(int j = 0 ; j < 6 ; j++){
if(now.v[j] != v[j]){
ex = false ;
break ;
}
}
if(ex) return true ;
}
return false ;
}

void shift(int[] v){
int t = v[0] ;
for(int i = 0 ; i <= 4 ; i++)
v[i] = v[i+1] ;
v[5] = t ;
}

boolean exsit = false ;
void doWork(int[] v){
int u = 0 ;
for(int i = 0 ; i < 6 ; i++){
u += v[i] ;
}
u %= Mod ;
for(int i = 0 ; i < 6 ; i++){
if(find(u , v)){
exsit = true ;
return ;
}
shift(v) ;
}
add(u, v) ;
for(int i = 0 ; i <= 2 ; i++){
int t = v[i] ;
v[i] = v[5-i] ;
v[5-i] = t ;
}
add(u , v) ;
}

void solve(){
Arrays.fill(head , -1) ;
int n = in.nextInt() ;
int[] v = new int[6] ;

while(n-- > 0){
for(int i = 0 ; i < 6 ; i++){
v[i] = in.nextInt() ;
}
if(! exsit) doWork(v) ;
}
out.println(exsit ? "Twin snowflakes found." : "No two snowflakes are alike.") ;
out.flush() ;
}
}

class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;

public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = new StringTokenizer("");
}

private void eat(String s) {
tokenizer = new StringTokenizer(s);
}

public String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}

public boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String s = nextLine();
if (s == null)
return false;
eat(s);
}
return true;
}

public String next() {
hasNext();
return tokenizer.nextToken();
}

public int nextInt() {
return Integer.parseInt(next());
}

public long nextLong() {
return Long.parseLong(next());
}

public double nextDouble() {
return Double.parseDouble(next());
}

public BigInteger nextBigInteger() {
return new BigInteger(next());
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: