Poj Solution 3692

http://poj.org/problem?id=3692

//* @author: <strong>Yeming&nbsp;Hu</strong>&quot;cslittleye@gmail.com&quot;
import java.util.*;

public class Main
{
    public static int b;
    public static int g;
    public static int m;
    public static boolean[][] graph;
    public static boolean[] checked;
    public static int[] link;

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
    graph = new boolean[200][200];
    checked = new boolean[200];
    link = new int[200];
    int t = 0;

    while(true)
    {
        b = sc.nextInt();
        g = sc.nextInt();
        m = sc.nextInt();

        if(b == 0 && g == 0 && m == 0)
        {
            break;
        }

        Arrays.fill(checked,false);
        Arrays.fill(link,-1);

        for(int i = 0; i < b; i++)
        {
            Arrays.fill(graph[i],true);
        }
        for(int i = 0; i < m; i++)
        {
            int a = sc.nextInt();
        int b = sc.nextInt();
        graph[a-1][b-1] = false;
        }
        int ans = 0;
        for(int i = 0; i < b; i++)
        {
            Arrays.fill(checked,false);
        if(find(i))
        {
            ans++;
        }
        }
        System.out.println("Case " + (++t) + ": " + (g+b-ans));
    }
    }

    public static boolean find(int a)
    {
        for(int i = 0; i < g; i++)
    {
        if(graph[a][i] && !checked[i])
        {
        checked[i] = true;
        if(link[i] == -1 || find(link[i]))
        {
            link[i] = a;
            return true;
        }
        }
    }
    return false;
    }
}

							
Posted in poj | Leave a comment

Poj Solution 3687

http://poj.org/problem?id=3687

/* @author: */
import java.util.Scanner;
import java.util.Arrays;
public class Main{
  int degree[]=new int[210];
  boolean visit[][]=new boolean[210][210];
  int path[]=new int[210];
  int n,m,t;

  void topsort(){
     for(int k=n;k>=1;k--){
          int i=n;
          while(i>=1 && degree[i]!=0) i--;
          if(i< 1){ System.out.printf("-1n"); return; }
          degree[i]=-1; 
          path[i]=k; 
          for(int x=1;x<=n;x++) if(visit[x][i]) degree[x]--;
     }    
     for(int j=1;j<=n;j++)  System.out.printf("%d ",path[j]);
     System.out.printf("n");     
}

 public void doit(){
   Scanner sc=new Scanner(System.in);
 
    int x,y;
    t=sc.nextInt();
    while((t--)!=0){
        n=sc.nextInt();
        m=sc.nextInt();
        Arrays.fill(degree,0);
        for(int i=0;i< visit.length;i++)
           Arrays.fill(visit[i],false);
        for(int i=1;i<=m;i++){
           x=sc.nextInt();
           y=sc.nextInt();
           if(!visit[x][y]){
              visit[x][y]=true;
              degree[x]++;              
            }  
        }       
        topsort();
    }
  }
 public static void main(String args[]){
   Main m=new Main();
     m.doit();
  }
}
							
Posted in poj | Leave a comment

Poj Solution 3677

http://poj.org/problem?id=3677

//* @author 
import java.util.*;
import java.io.*;

class Main
{
 static int armor[];
 final static int MAX = 1000000;
 static class Node
 {
    Node l = null, r = null;
    int flag, step, value;
    Node(int k, int v)
    {
         flag = 0;
     step = MAX;
     value = v;
     if(k>=0&&v<=30)
     {
        l = new Node(k, v+armor[k]);
        r = new Node(k-1, v);
     }
    }
    

  int update(int f, int v, int s)
  {
    if(value>v||l==null&&r==null)return MAX;
    if(f==flag)step = Math.min(s, step);
    else step = s;
    s = step;
    flag = f+1;
    //System.err.println(value+":"+s);
    if(value==v)return s;
    return step = Math.min(l.update(f, v, s+1)+1, r.update(f, v, s));
  }
  int query(int v)
  {
    if(l==null&&r==null)return MAX;
    if(value==v)return step;
    return Math.min(l.query(v), r.query(v));
  }
 }

 public static void main(String[] args) throws IOException
 {
  Scanner in = new Scanner(System.in);
  for(int T = in.nextInt(); T!=0; --T)
  {
    int n = in.nextInt();
    armor = new int[n];
    for(int i=0; i!=n; ++i)
        armor[i] = in.nextInt();
    Arrays.sort(armor);
    int cnt=0;
    Node root = new Node(n-1, 0);
    root.step = 0;
    int k = 0, m = in.nextInt();
    for(int i=0; i!=m; ++i)
        root.update(cnt++, k=in.nextInt(), MAX);
    k = root.query(k);
    if(k==MAX)System.out.println(-1);
    else System.out.println(k);
    }
  }
}

							
Posted in poj | Leave a comment

Poj Solution 3673

http://poj.org/problem?id=3673

import java.util.Scanner;
public class Main{
 public static void main(String[] args) {
  Scanner scanner=new Scanner(System.in);
  String str1=scanner.next();
  String str2=scanner.next();
  char[] array1=str1.toCharArray();
  char[] array2=str2.toCharArray();
  int sum1=0;
  for(int i=0;i< array1.length;i++){
    sum1+=array1[i]-48;
   }

  int sum2=0;
  for(int i=0;i< array2.length;i++){
   sum2+=array2[i]-48;
  }

  System.out.println(""+(sum1*sum2));
 }
}
							
Posted in poj | Leave a comment

Poj Solution 3672

http://poj.org/problem?id=3672

//* @author ������&lt;hongxp11@163.com&gt;
import java.util.Scanner;


public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int M = in.nextInt();
        int T = in.nextInt();
        int U = in.nextInt();
        int F = in.nextInt();
        int D = in.nextInt();
        int num = 0;
        int sum = 0;
        for(int i = 0; i < T; i++)
        {
            String s = in.next();
            if(sum > M)
                break;
            if(s.equals("u") || s.equals("d"))
                sum += U + D;
            else if(s.equals("f"))
                sum += F + F;
            num++;
        }
        System.out.println(num - 1);
    }

}

							
Posted in poj | Leave a comment

Poj Solution 3671

http://poj.org/problem?id=3671

import java.util.Scanner;
import java.util.Arrays;
public class Main{
  
 public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
  int n;
  int v[]=new int[30002];
  int f[]=new int[30002];
  int g[]=new int[30002];
  n=sc.nextInt();
  for(int i=1;i<=n;i++)
    v[i]=sc.nextInt();
  
 f[0]=0; g[n+1]=0;
 for(int i=1;i<=n;i++){
     if(v[i]==2) f[i]=f[i-1]+1;
      else f[i]=f[i-1];     
  }
  for(int i=n;i>=1;i--){
      if(v[i]==1) g[i]=g[i+1]+1;
       else g[i]=g[i+1];       
  }
  int mins=100000000;
  for(int i=0;i<=n;i++)
      if(f[i]+g[i+1]< mins)
  mins=f[i]+g[i+1];
 System.out.printf("%dn",mins);
 }
}
							
Posted in poj | Leave a comment

Poj Solution 3670

http://poj.org/problem?id=3670

/* @author: */
import java.util.Scanner;
import java.util.Arrays;
public class Main{
  static int mintwo(int a,int b){ return a< b?a:b; }
  static int minthird(int a,int b,int c){
    int temp=mintwo(a,b);
    return mintwo(temp,c);   
} 

 public static void main(String args[]){
   Scanner sc=new Scanner(System.in);
 
   int f[][]=new int[30010][3];
   int g[][]=new int[30010][3];
   int v[]=new int[30010];
   int n=sc.nextInt();
    
  for(int i=1;i<=n;i++){
   v[i]=sc.nextInt();  
   f[i][0]=f[i-1][0];
   if(v[i]!=1) f[i][0]++;
   f[i][1]=mintwo(f[i-1][0],f[i-1][1]);
   if(v[i]!=2) f[i][1]++;
   f[i][2]=minthird(f[i-1][0],f[i-1][1],f[i-1][2]);
   if(v[i]!=3) f[i][2]++;                 
  }
     for(int i=1;i<=n;i++){
        g[i][2]=g[i-1][2];
        if(v[i]!=3) g[i][2]++;
        g[i][1]=mintwo(g[i-1][2],g[i-1][1]);
        if(v[i]!=2) g[i][1]++;
        g[i][0]=minthird(g[i-1][2],g[i-1][1],g[i-1][0]);
        if(v[i]!=1) g[i][0]++;        
     }
     int mins=100000000;
     for(int i=0;i< 3;i++){
          if(mins>f[n][i]) mins=f[n][i];
          if(mins>g[n][i]) mins=g[n][i];       
     }
     System.out.printf("%dn",mins);
   }
}
							
Posted in poj | Leave a comment

Poj Solution 3665

http://poj.org/problem?id=3665

/* @author: */
import java.util.Scanner;
import java.util.Arrays;
public class Main{
 
 public static void main(String args[])
{
 Scanner sc=new Scanner(System.in);
 int arr[]=new int[10010];
 int n,m,i,j,u;
 n=sc.nextInt();
 m=sc.nextInt();
 for(i=0;i< n;i++)
    arr[i]=sc.nextInt();
 for(i=0;i< m;i++)
 {
   int maxx=-1,tag=0;
   for(j=0;j< n;j++)
    if(arr[j]>maxx)
    {
    maxx=arr[j];
    tag=j;
     }
   System.out.printf("%dn",tag+1);
   int add=arr[tag]/(n-1);
   int mod=arr[tag]%(n-1);
   for(j=0;j< n;j++)
    arr[j]+=add;
   for(j=0;j< mod;j++)
   {
    arr[j]++;
    if(j==tag) mod++;
   }
   arr[tag]=0;
  }
 }
}
							
Posted in poj | Leave a comment

Poj Solution 3664

http://poj.org/problem?id=3664

//* @author ������&lt;hongxp11@163.com&gt;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
    static class vote {
        int first;
        int second;
        int index;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int K = in.nextInt();
        // vote[] voters = new vote[N];
        ArrayList< vote> voters = new ArrayList< vote>();
        for (int i = 0; i < N; i++) {
            vote temp = new vote();
            temp.first = in.nextInt();
            temp.second = in.nextInt();
            temp.index = i + 1;
            voters.add(temp);
        }
        Collections.sort(voters, new Comparator< Object>() {
            public int compare(Object o1, Object o2) {
                vote a = (vote) o1;
                vote b = (vote) o2;
                return b.first - a.first;
            }

        });
        
        int result = 0;
        for (int i = 1; i < K; i++) {
            if (voters.get(result).second < voters.get(i).second) {
                
                result = i;
            }
        }
        System.out.println(voters.get(result).index);
    }
}

							
Posted in poj | Leave a comment

Poj Solution 3663

http://poj.org/problem?id=3663

//* @author: 82638882@163.com
import java.util.*;
public class Main
{
 public static void main(String[] args)
 {
  Scanner in=new Scanner(System.in);
  int a=in.nextInt();
  int b=in.nextInt();
  ArrayList< Integer> arr=new ArrayList< Integer>();
  for(int i=0;i< a;i++)
    arr.add(in.nextInt());
  Collections.sort(arr);
  int count=0;
  for(int i=0;i< a-1;i++)
  {
    int l=b-arr.get(i);
    int min=i+1;
    int max=a-1;
    boolean bb=false;
    while(max>min)
    {
        int mid=(max+min)/2;
        if(arr.get(mid)>l) max=mid-1;
        else if(arr.get(mid)<=l) min=mid+1;
        bb=true;
        
    }
    if(bb)
    {
        if(arr.get(min)>l) count--;
        count+=min-i;
    }
    
    }
    System.out.println(count);
  }
}

							
Posted in poj | Leave a comment

Poj Solution 3660

http://poj.org/problem?id=3660

//* @author popop0p0popo
import java.util.*;
import java.io.*;

public class Main{
 public static void main(String[] args){
  Scanner in=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int n=in.nextInt();
        int m=in.nextInt();
        int[][] p=new int[n][n];
        for (int i=0;i< n ;i++ ){
            for (int j=0;j< n ;j++ ){
                p[i][j]=101;
            }
            p[i][i]=0;
        }
        for (int i=0;i< m ;i++ ){
            p[in.nextInt()-1][in.nextInt()-1]=1;
        }
        for (int k=0;k< n ;k++ ){
            for (int i=0;i< n ;i++ ){
                for (int j=0;j< n ;j++ ){
                 p[i][j]=getMin(p[i][j],p[i][k]+p[k][j]);
                }
            }
        }
        int w=0;
        for (int i=0;i< n ;i++ ){
            int t=0;
            for (int j=0;j< n ;j++ ){
                if (p[i][j]< 101||p[j][i]< 101){
                    t++;
                }
            }
            if (t==n){
                w++;
            }
        }
        System.out.println(w);
    }

    public static int getMin(int a,int b){
        if (a>=b){
            return b;
        }
        else{
            return a;
        }
    }
}


							
Posted in poj | Leave a comment

Poj Solution 3658

http://poj.org/problem?id=3658

//* @author 
/**
 * @version 2009/08/21
 * @author sbzlyessit
 */

import java.io.*;
import java.util.*;

public class Main {

    private static final int        MAX_N = 100000;

    private static BufferedReader   in =new BufferedReader(new InputStreamReader(System.in));
    private static StringTokenizer  st;
    private static int[]            height = new int[MAX_N];
    private static long[]           width = new long[MAX_N + 1];
    private static long[]           area = new long[MAX_N + 1];
    private static int[]            l = new int[MAX_N], r = new int[MAX_N];
    private static int[]            stack = new int[MAX_N + 1];
    private static int[]            min = new int[MAX_N], max = new int[MAX_N];
    private static long[]           base = new long[MAX_N];
    private static boolean[]        used = new boolean[MAX_N];
    private static long[]           result = new long[MAX_N];
    private static int              N, pos, root;

    public static void main(String[] argv) throws Exception {
        readIn();
        solve();
    }

    private static void readIn() throws Exception {
        int top = 0, minH = Integer.MAX_VALUE;
        int i, x;
        N = nextInt();
        width[0] = area[0] = 0;
        for (i = 0; i < N; i++) {
            x = nextInt();
            height[i] = nextInt();
            width[i + 1] = width[i] + x;
            area[i + 1] = area[i] + (long) x * height[i];
            if (height[i] < minH) {
                minH = height[i];
                pos = i;
            }
            for (stack[top] = -1; top > 0 && height[stack[top - 1]] < height[i]; top--);
            if (top > 0) {
                r[stack[top - 1]] = i;
            }
            l[i] = stack[top] != -1 ? stack[top] : -1;
            r[i] = -1;
            stack[top++] = i;
        }
        root = stack[0];
    }

    private static void solve() {
        int top = 0;
        int i, x, y;
        stack[top++] = root;
        min[root] = 0; max[root] = N;
        base[root] = 0;
        while (top > 0) {
            x = stack[top - 1];
            if (!used[x]) {
                result[x] = base[x] + (width[max[x]] - width[min[x]]) * (height[x] + 1) - 
                       area[max[x]] + area[min[x]];
                used[stack[top - 1]] = true;
            }
            if ((y = getChild(x)) == 0) {
                top--;
            } else if (y < 0) {
                stack[top++] = l[x];
                min[l[x]] = min[x]; max[l[x]] = x;
                base[l[x]] = base[x] + (pos > x ? (width[max[x]] - width[x + 1]) * height[x] -
                        area[max[x]] + area[x + 1] : 0);
            } else {
                stack[top++] = r[x];
                min[r[x]] = x + 1; max[r[x]] = max[x];
                base[r[x]] = base[x] + (pos < x ? (width[x] - width[min[x]]) * height[x] -
                        area[x] + area[min[x]] : 0);
            }
        }
        for (i = 0; i < N; i++) {
            System.out.println(result[i]);
        }
    }

    private static int getChild(int x) {
        if (l[x] != -1 && !used[l[x]]) {
            return -1;
        } else if (r[x] != -1 && !used[r[x]]) {
            return 1;
        }
        return 0;
    }

    private static int nextInt() throws Exception {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return Integer.parseInt(st.nextToken());
    }

}


							
Posted in poj | Leave a comment

Poj Solution 3657

http://poj.org/problem?id=3657

 
							
Posted in poj | Leave a comment

Poj Solution 3653

http://poj.org/problem?id=3653

//* @author: SmilingWang
import java.util.*;
import java.io.*;

public class Main {
 static final int INF = 100000000;
 public static void main(String[] args){
   Scanner in = new Scanner(new BufferedInputStream(System.in));
   int x = in.nextInt();
   int y = in.nextInt();
   while(x != 0 || y != 0){
    int w[][] = new int[(x+1)*(y+1)+1][(x+1)*(y+1)+1];
    //int num = 1;
    int row = 1;
    for(int i = 1; i <= 2*x+1; i++){
        if(i % 2 == 1){
       //num = (row-1)*(y+1) + 1;
       for(int j = 1; j <= y; j++){
        int tm = in.nextInt();
        String tms = in.next();
                        
        if(tms.equals("*")){
          w[(row-1)*(y+1)+j][(row-1)*(y+1)+j+1] = tm;
          w[(row-1)*(y+1)+j + 1][(row-1)*(y+1)+j] = tm;
        }
        else if(tms.equals("< ")){
          w[(row-1)*(y+1)+j + 1][(row-1)*(y+1)+j] = tm;
        }
        else if(tms.equals(">")){
          w[(row-1)*(y+1)+j][(row-1)*(y+1)+j+1] = tm;
        }
        }
        row++;
      }
     else{
       for(int j = 1; j <= y + 1; j++){
        int tm = in.nextInt();
        String tms = in.next();
            
          if(tms.equals("*")){
        w[(row-2)*(y+1) + j][(row-1)*(y+1) + j] = tm;
        w[(row-1)*(y+1) + j][(row-2)*(y+1) + j] = tm;
       }
      else if(tms.equals("^")){
        w[(row-1)*(y+1) + j][(row-2)*(y+1) + j] = tm;
      }
      else if(tms.equals("v")){
        w[(row-2)*(y+1) + j][(row-1)*(y+1) + j] = tm;
      }
    }
    }
   }
  for(int p = 1; p <= (x+1)*(y+1); p++){
   for(int q = 1; q <= (x+1)*(y+1); q++){
    if(p == q){
         continue;
    }
    if(w[p][q] > 0){
      w[p][q] = 2520/w[p][q];
      continue;
    }
    w[p][q] = INF;
     }
   }
            
   dijkstra(w, (x+1)*(y+1));            
   x = in.nextInt();
   y = in.nextInt();
 }
}

public static void dijkstra(int w[][], int v){
  int i, vnear = 0;
  int touch[] = new int[v+1];
  int length[] = new int[v+1];
  boolean y[] = new boolean[v+1];
  int map[] = new int[v+1];
  PriorityQueue< Item> heap = new PriorityQueue< Item>(v, new ItemCompare());
        
  for(i = 2; i <= v; i++){
   length[i] = w[1][i];
   if(length[i] < INF){
    heap.add(new Item(1, i, w[1][i]));
   }
  }
  //System.out.println(heap);
  while(!heap.isEmpty()){
    Item temp = heap.remove();
            
    for(i = 1; i <= v; i++){
        if(i == temp.sv){
        continue;
      }
      else if(w[temp.ev][i] == INF){
        continue;
      }
      if(length[temp.ev] + w[temp.ev][i] < length[i]){
       length[i] = length[temp.ev] + w[temp.ev][i];
       heap.add(new Item(temp.ev, i, length[i]));
      }
     }
    }
    if(length[v] < INF){
      System.out.println(length[v] + " blips");
    }
    else{
      System.out.println("Holiday");
    }
    }
    
}

class Item{
 int sv;
 int ev;
 int w;
    
 Item(int sv, int ev, int w){
    this.sv = sv;
    this.ev = ev;
    this.w  = w;
   }
    
  public String toString(){
    return this.sv + " " + this.ev +" " + this.w;
  }
}

class ItemCompare implements Comparator< Item>{
    public int compare(Item i1, Item i2){
        return (i1.w - i2.w);
    }
}

							
Posted in poj | Leave a comment

Poj Solution 3652

http://poj.org/problem?id=3652

 import java.util.Arrays;
 import java.util.Scanner;

 public class Main {

     int a;
     int b;
     int c;
     int s;
     int x;
     char[] result;
     int flg;
     boolean[] dup;

     public Main() {
         Scanner scan = new Scanner(System.in);
         while ((a = scan.nextInt()) != 0) {
             b = scan.nextInt();
             c = scan.nextInt();
             s = scan.nextInt();
             result = new char[16];
             Arrays.fill(result, '0');
             initFlg(Integer.toBinaryString(s));
             dup = new boolean[c];
             dup[s] = true;
             x = (a * s + b) % c;
             while (!dup[x]) {
                 dup[x] = true;
                 if (setFlg(Integer.toBinaryString(x))) {
                     break;
                 }
                 x = (a * x + b) % c;
             }
             System.out.println(result);
         }
     }

     public void initFlg(String str) {
         for (int i = 15, k = str.length() - 1; k >= 0; i--, k--) {
             if (str.charAt(k) == '1') {
                 result[i] = '1';
             }
         }
     }

     public boolean setFlg(String str) {
         flg = 0;
         for (int i = 15, k = str.length() - 1; k >= 0; i--, k--) {
             if (result[i] == '?') {
                 flg++;
                 if (flg == 16) {
                     return true;
                 }
                 continue;
             } else {
                 if (str.charAt(k) != result[i]) {
                     result[i] = '?';
                 }
             }
         }
         return false;
     }

     public static void main(String[] args) {
         new Main();
     }

 } 

							
Posted in poj | Leave a comment

Poj Solution 3650

http://poj.org/problem?id=3650

//* @author ������&lt;hongxp11@163.com&gt;
import java.util.Scanner;


public class Main {
 public static void main(String[] args)
 {
  Scanner in = new Scanner(System.in);
   while(true)
    {
       String input = in.nextLine();
    if(input.equals("#"))
        break;
    int len = input.length();
    for(int i = 0; i < len; i++)
    {
        if(input.charAt(i)==' ')
            System.out.print("%20");
        else if(input.charAt(i)=='!')
            System.out.print("%21");
        else if(input.charAt(i)=='$')
            System.out.print("%24");
        else if(input.charAt(i)=='%')
            System.out.print("%25");
        else if(input.charAt(i)=='(')
            System.out.print("%28");
        else if(input.charAt(i)==')')
            System.out.print("%29");
        else if(input.charAt(i)=='*')
            System.out.print("%2a");
        else
            System.out.print(input.charAt(i));
    }
    System.out.println();
    }
  }
}

							
Posted in poj | Leave a comment

Poj Solution 3646

http://poj.org/problem?id=3646

//* @author: 
import java.util.*;
public   class Main{    
   
    public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
 
    int i,j,ans;
    end: while (sc.hasNext()) {
        int n=sc.nextInt();
        if(n==0) break;
        int m=sc.nextInt();
        int a[]=new int[n+1];
        int b[]=new int[m+1];
       
        for ( i=1;i<=n;i++) 
           a[i]=sc.nextInt();
        for ( i=1;i<=m;i++) 
           b[i]=sc.nextInt();
        Arrays.sort(a, 1,n+1);
        Arrays.sort(b,1,m+1);
        for (ans=0,j=i=1;i<=n;i++) {
        if (j>m) {
             System.out.println("Loowater is doomed!");
              continue end;
         }
            while (a[i]>b[j]) {
              j++;if (j>m) {
                System.out.println("Loowater is doomed!");
                continue end;
              }
            }
            ans+=b[j];j++;
        }
        System.out.printf("%dn",ans);
       
    }
  }
}
							
Posted in poj | Leave a comment

Poj Solution 3641

http://poj.org/problem?id=3641

//* @author: 82638882@163.com
import java.util.Scanner;
public class Main
{
 public static void main(String[] args)
 {
  Scanner in=new Scanner(System.in);
  while(true)
  {
    long p=in.nextLong();
    long a=in.nextLong();
    if(a==0&&p==0) break;
    if(isPrime(p)){
        System.out.println("no");
        continue;
    }
    System.out.println(sum(a,p,p)==a?"yes":"no");
   }
  }

 public static boolean isPrime(long a)
 {
    if(a==2) return true;
    for(int i=3;i<=Math.sqrt(a);i+=2)
        if(a%i==0) return false;
    return true;
  }

 public static long sum(long a,long n,long p)
 {
    if(n==0) return 1;
    long w=sum(a*a%p,n/2,p);
    if(n%2==1) w=(w*a)%p;
    return w;
 }
}
							
Posted in poj | Leave a comment

Poj Solution 3640

http://poj.org/problem?id=3640

//* @author ������&lt;hongxp11@163.com&gt;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Main {

/**
 * @param args
 * @throws IOException
 * @throws NumberFormatException
 */
public static void main(String[] args) throws NumberFormatException,
    IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  while (true) {
    Map< String, Integer> map = new HashMap< String, Integer>();
    int max = 0;
    int n = Integer.parseInt(br.readLine());
    if (n == 0)
        break;
    for (int i = 0; i < n; i++) {
        String s = br.readLine();
        String[] array = s.split(" ");
        Arrays.sort(array);
        String key = new String();
        for (int j = 0; j < array.length; j++) {
            key += array[j];
        }
        // System.out.println("key:"+key);
        if (map.get(key) == null)
            map.put(key, 1);
        else {
            // System.out.println("here");
            map.put(key, map.get(key) + 1);
        }
        if (max < map.get(key))
            max = map.get(key);
    }
    int result = 0;
    Set<String> set = map.keySet();
    for (String s : set) {
        if (max == map.get(s))
            result += max;
    }
    System.out.println(result);
  }
 }

}

							
Posted in poj | Leave a comment

书单

sight_stop2sanyan_erpailinux_kernaldragon_bookbcasm+32

Posted in books, 未分类 | Leave a comment

八佾

bayi

Posted in 默写 | Leave a comment

入门者学

heart

Posted in 默写 | Leave a comment

泰伯

taibo

Posted in 默写 | Leave a comment

学而

talk

Posted in 默写 | Leave a comment

为政

weizheng

Posted in 默写 | Leave a comment

乡党

xiangdang

Posted in 默写 | Leave a comment

先进

xianjin_01
xianjin

Posted in 默写 | Leave a comment

宪问

xianwen

Posted in 默写 | Leave a comment

大师兄

yanyuan

Posted in 默写 | Leave a comment

公冶长

yecangself

Posted in 默写 | Leave a comment

雍也

yongye

Posted in 默写 | Leave a comment

子罕

zihan

Posted in 默写 | Leave a comment

季路

zilun

Posted in 默写 | Leave a comment

ORM设计

首希望本文的读者拥有一定的ORM框架设计经验,或者熟悉ORM原理,最好是自己动手实现过ORM映射
如果没有请参考Doctrine或者Zend DB实现
在传统的ORM框架中都会有这样一个需求:"将一个一维数组或二维数组插入或替换到数据库中"
关于一维数组的处理这里不讨论,这里只关心二维数组
如下一个二维数组(这里使用php编码比较通俗易懂),假设某张表有三个字段 a,b,c
$arr=array(
array(
'a'=>1,
'b'=>2,
),
array(
'a'=>3,
'b'=>4,
)
);
这种数组每一维键和键顺序完全相同 只要取出第一维键值作为 INSERT INTO `table_name`(#####) 括号里的键值 然后把所有维的值拼起来作为
VALUES部分就解决了,问题是不是所有程序员都会遵循使用规则,有可能传给你一个这样的数组
$arr=array(
array(
'a'=>1,
'b'=>2,
),
array(
'a'=>3,
'b'=>4,
'c'=>5,
)
);
这样就会导致那个SQL初学者常见的columns does not match values错误,这时候我们应该使用一个函数判断一个函数是否是完全符合二维数组标准,类似如下
function is_standard_2darray($array)
{
$current=current($array);
//第一维必须为数组
if(is_array($current))
{
//这里使用crc32作为唯一校验,当然也可以不用
$crc=crc32(join('', array_keys($current)));
foreach ($array as $k=>$v)
{
$lcrc=crc32(join('', array_keys($v)));
if($lcrc!=$crc)
{
return FALSE;
}
}
}else{
return FALSE;
}
return TRUE;
}
这样可以检测到数组是否是标准二维数组,如果是那直接用第一种方法处理,如果不是那就需要将数组进行按键值分类,使用类似如下函数
function classify($array) {
$ret = array ();
$current = current ( $array );
if (is_array ( $current )) {
foreach ( $array as $k=>$v )
{
$lcrc = crc32 ( join ( '', array_keys ( $v ) ) );
//按数组键值CRC进行分类可以保证顺序也是对的
!isset($ret[$lcrc])&&$ret[$lcrc]=array();
$ret[$lcrc][]=$v;
}
}
return $ret;
}
这样将一个二维数组变成一个三维数组,其中每个第二维都是一个整理好的可以一次性插入而数据库不会报错的数据
然后遍历该数组将每一维作为参数再调用一下自身
完美解决PS
BTW
原本的框架设计原则是 "在易用性和可扩展性之间寻找一个平衡",关于这点我很佩服BOOST那伙人.
因为我们一开始并不知道一个系统最终是什么样子的,所以我们没办法对系统做更高程度的抽象(其实就是重构啦)
如此做的结果是虽然框架很智能,但这在耗费性能的同时还把风险留给了程序员,本来应该遵守数据一致性的原则会变的不那么严谨
姑且也算找到一个平衡点吧

Posted in 默写 | Leave a comment

vps 升级 数据库丢了

没了就没了吧 没有备份………………

Posted in 未分类 | Leave a comment

Poj Solution 3639

http://poj.org/problem?id=3639

//* @author: 
import java.util.*;
public   class Main{    
   
    public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
      
    int i,n;
    double rate,us,ca,tus,tca,t1,t2;
    while (sc.hasNext()) {
        n=sc.nextInt();
        if(n==0) break;
        ca=1000;us=0;
        for (i=1;i<=n;i++) {
            rate=sc.nextDouble();
            t1=ca/rate*.97;
            t1=(Math.floor(t1*100))/100;
            t2=us*rate*.97;
            t2=(Math.floor(t2*100))/100;
            tus=(us>t1?us:t1);
            tca=(ca>t2?ca:t2);
            us=tus;ca=tca;
        }
        System.out.printf("%.2fn",ca);
    }
  }
    
}

 


							
Posted in poj | Leave a comment

Poj Solution 3637

http://poj.org/problem?id=3637

//* @author popop0p0popo
import java.util.*;
import java.io.*;

public class Main{
 public static void main(String[] args){
  Scanner scanner=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int n=scanner.nextInt();
        int t,total;
        int[] prs;
        for (int i=0;i< n ;i++ ){
            t=scanner.nextInt();
            prs=new int[t];
            for (int j=0;j< t ;j++ ){
                prs[j]=scanner.nextInt();
            }
            Arrays.sort(prs);
            total=0;
            for (int j=t-1;j>=0 ;j-- ){
                if ((t-j)%3==0){
                    total=total+prs[j];
                }
            }
            System.out.println(total);
        }
    }
}


							
Posted in poj | Leave a comment

Poj Solution 3633

http://poj.org/problem?id=3633

//* @author 
/**
 * @version 2009/08/26
 * @author sbzlyessit
 */

 /*������. ״̬����, ���ü��仯����, ����ת�ƺķѾ޴�, ���������֦. 
  *����Ҫ��һ���֦����ÿһλƥ��ʱ��Ҫȡ��ij��Ƚ�����һ������. 
  */

import java.io.*;
import java.util.*;

public class Main {

    private static final int        MAX_N = 18;

    private static BufferedReader   in =
        new BufferedReader(new InputStreamReader(System.in));
    private static char[]           s, t, sr;
    private static int[]            matchs, matchsr;
    private static int[]            hash = new int[1 <<  MAX_N];

    public static void main(String[] argv) throws Exception {
        for(int caseSize = Integer.parseInt(in.readLine()); caseSize > 0; caseSize--){
            if (!readIn()) {
                System.out.println("impossible");
            } else {
                System.out.println(solve(0));
            }
        }
    }

    private static boolean readIn() throws Exception {
        String  S = in.readLine(), T = in.readLine();
        int     i;
        if ((!S.contains("A") && T.contains("A")) || (!S.contains("C") && T.contains("C")) ||
                (!S.contains("G") && T.contains("G")) || (!S.contains("T") && T.contains("T"))) {
            return false;
        }
        s = S.toCharArray();
        t = T.toCharArray();
        sr = new char[s.length];
        matchs = new int[t.length];
        matchsr = new int[t.length];
        Arrays.fill(hash, Integer.MAX_VALUE);
        hash[(1 <<  t.length) - 1] = 0;
        reverse(s, sr);
        for (i = 0; i <  t.length; i++) {
            matchs[i] = longestMatch(i, t.length, s);
            matchsr[i] = longestMatch(i, t.length, sr);
        }
        return true;
    }

    private static void reverse(char[] a, char[] ar) {
        for (int i = 0; i < a.length; i++) {
            ar[i] = a[a.length - i - 1];
        }
    }

    private static int longestMatch(int from, int to, char[] a) {
        int i, len, max = 0;
        for (i = 0, len = 0; i < a.length; i++, len = 0) {
            while (len < to - from && i + len < a.length && a[i + len] == t[from + len]) {
                len++;
            }
            max = Math.max(max, len);
        }
        return max;
    }

    private static int solve(int state) {
        if (hash[state] != Integer.MAX_VALUE) {
            return hash[state];
        }
        char[] done = new char[t.length];
        char[] doner = new char[t.length];
        int i, m = 0, len, g;
        for (i = 0; i < done.length; i++) {
            done[i] = (state & (1 << i)) != 0 ? t[i] : '0';
        }
        reverse(done, doner);
        for(i = 0; i < t.length; i++) {
            if ((state & (1 << i)) != 0) {
                m = 0;
                continue;
            }
            len = 1;
            while (i + len < t.length && (state & (1 << (i + len))) == 0) {
                len++;
            }
            g = Math.min(len, Math.max(matchs[i], matchsr[i]));
            g = Math.max(g, longestMatch(i, i + len, done));
            g = Math.max(g, longestMatch(i, i + len, doner));
            if(g > m - 1) {
                hash[state] = Math.min(hash[state], 1 + solve(state | ((1 << (i + g)) - (1 << i))));
            }
            m = g;
        }
        return hash[state];
    }

}


							
Posted in poj | Leave a comment

Poj Solution 3632

http://poj.org/problem?id=3632

//* @author: 82638882@163.com
import java.util.Scanner;
public class Main
{
 public static void main(String[] args)
 {
  Scanner in=new Scanner(System.in);
  int a=in.nextInt();
  while(a!=0)
  {
    int b=in.nextInt();
    int[] c=new int[b];
    for(int i=0;i< b;i++)
    {
        c[i]=in.nextInt();
    }
    int d,e,g;
    for(d=e=c[0],g=0;g< b;g++)
    {
        if(d>c[g])d=c[g];
        if(e< c[g])e=c[g];
    }
    System.out.println((e-d)*2);
    a--;
    }
  }
}
							
Posted in poj | Leave a comment

Poj Solution 3630

http://poj.org/problem?id=3630

import java.util.Scanner;   
  
public class Main {   
    private static Trie trie = new Trie();   
    private static boolean isConsistent = true;   
  
    public static void main(String[] args) {   
        Scanner sc = new Scanner(System.in);   
        int t = sc.nextInt();   
        for (int i = 0; i < t; ++i) {   
            int n = sc.nextInt();   
            sc.nextLine();   
            trie = new Trie();   
            isConsistent = true;   
            for (int j = 0; j < n; ++j) {   
                String phone = sc.nextLine();   
                if (isConsistent)   
                    buildTrie(phone);   
            }   
            if (isConsistent)   
                System.out.println("YES");   
            else  
                System.out.println("NO");   
        }   
    }   
  
    static void buildTrie(String s) {   
        // char[] c = s.toCharArray();   
        int len = s.length();   
        Trie tmpTrie = trie;   
        for (int i = 0; i < len; ++i) {   
            int ch = Integer.parseInt(s.substring(i, i + 1));   
            // System.out.println(ch+"---"+(int)ch);   
            Trie tmp = tmpTrie.next[ch];   
            if (tmp == null) {   
                tmp = new Trie();   
                tmp.node = 1;   
                if (i == len - 1)   
                    tmp.isDispear = true;// 表示这是一个电话的结尾   
                tmpTrie.next[ch] = tmp;   
                tmpTrie = tmp;   
            } else {   
                if (tmp.isDispear) {   
                    isConsistent = false;   
                    break;   
                }   
  
                if (i == len - 1) {   
                    isConsistent = false;   
                    break;   
                }   
                tmpTrie = tmp;   
            }   
        }   
    }   
  
    private static class Trie {   
        int node = 0;   
        boolean isDispear = false;//是否是某个电话号码的最后一个   
        Trie[] next = new Trie[10];   
    }   
}  

//出处:<a href="http://blog.csdn.net/lazy_p/archive/2010/06/21/5683414.aspx" target="_blank">http://blog.csdn.net/lazy_p/archive/2010/06/21/5683414.aspx</a>

							
Posted in poj | Leave a comment

Poj Solution 3628

http://poj.org/problem?id=3628

 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.util.Arrays;

 public class Main {

     static int n, b;
     static int[] h;
     static boolean[] used;
     static int min = Integer.MAX_VALUE;

     public static void main(String[] args) throws Exception {
         BufferedReader read = new BufferedReader(new InputStreamReader(
                 System.in));
         String[] s = read.readLine().split(" ");
         n = Integer.parseInt(s[0]);
         h = new int[n];
         used = new boolean[n];
         b = Integer.parseInt(s[1]);
         for (int i = 0; i < n; i++) {
             h[i] = Integer.parseInt(read.readLine());
         }
         Arrays.sort(h);
         search(0, 0);
         System.out.println(min);
     }

     public static boolean search(int sum, int deep) {
         if (sum >= b) {
             if (sum - b < min) {
                 min = sum - b;
             }
             return true;
         }
         for (int i = deep; i < n; i++) {
             if (used[i]) {
                 continue;
             }
             used[i] = true;
             if (search(sum + h[i], i + 1)) {
                 used[i] = false;
                 break;
             }
             used[i] = false;
         }
         return false;
     } 
}

							
Posted in poj | Leave a comment

Poj Solution 3627

http://poj.org/problem?id=3627

//* @author 
import java.io.*; 
import java.util.*; 
import java.math.*; 
public class Main 
{ 
    static int[] bookHeight; 
    static int n; 
    static BigInteger b; 
    public static void main(String[] args) throws Exception 
    { 
        readFile(); 
    }    
    public static void readFile() throws Exception 
    { 
        BufferedReader br = new BufferedReader( 
            new InputStreamReader(System.in)); 
        StringTokenizer st = new StringTokenizer( 
            br.readLine()," "); 
        n = Integer.valueOf(st.nextToken()); 
        b = new BigInteger(st.nextToken()); 
        int iCount = 0; 
        bookHeight = new int[n]; 
        while(iCount< n) 
        { 
            bookHeight[iCount++] = Integer.valueOf( 
                br.readLine());  
        } 
        process(); 
    } 
    public static void defbug() 
    { 
        for(int i=0; i< bookHeight.length; i++) 
            System.out.println(bookHeight[i]+" "); 
        System.out.println(); 
    } 
    public static void process() 
    { 
        Arrays.sort(bookHeight); 
        BigInteger sum = BigInteger.valueOf(0L); 
        BigInteger temp; 
        int iCount = 0; 
        for(int i=bookHeight.length-1; i>=0; i--) 
        { 
            sum = sum.add(BigInteger.valueOf( 
                    (long)(bookHeight[i]))); 
            temp = sum.max(b); 
            iCount++; 
            if(temp.equals(sum)) 
                break;   
        } 
        System.out.print(iCount); 
    } 
}
							
Posted in poj | Leave a comment

Poj Solution 3626

http://poj.org/problem?id=3626

//* @author:

import java.io.BufferedInputStream;
import java.util.LinkedList;
import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
public class Main {

    static int[][] field = new int[1001][1001];
    static int y;
    static int n;
    static int x;

    public static void main(String[] args) {
        Scanner scan = new Scanner(new BufferedInputStream(System.in));
        x = scan.nextInt();
        y = scan.nextInt();
        n = scan.nextInt();
        field[x + 500][y + 500] = 1;
        for (int i = 0; i < n; i++) {
            int a = scan.nextInt();
            int b = scan.nextInt();
            field[a + 500][b + 500] = -1;
        }
        solve();
    }

    public static void solve() {
        System.out.println(bsf(500, 500));
    }
    public static int bsf(int r, int c) {

        LinkedList< Point> queue = new LinkedList();
        queue.add(new Point(r, c, 0));
        field[r][c] = -1;

        while (!queue.isEmpty()) {
            Point current = queue.removeFirst();
            int ai = current.a;
            int bi = current.b;

            if (ai - 1 >= 0 && field[ai - 1][bi] == 0) {
                queue.addLast(new Point(ai - 1, bi, current.step + 1));
                field[ai - 1][bi] = -1;
            } else if (field[ai - 1][bi] == 1) {
                return current.step + 1;
            }

            if (ai + 1 <= 1000 && field[ai + 1][bi] == 0) {
                queue.addLast(new Point(ai + 1, bi, current.step + 1));
                field[ai + 1][bi] = -1;
            } else if (field[ai + 1][bi] == 1) {
                return current.step + 1;
            }
            if (bi - 1 >= 0 && field[ai][bi - 1] == 0) {
                queue.addLast(new Point(ai, bi - 1, current.step + 1));
                field[ai][bi - 1] = -1;
            } else if (field[ai][bi - 1] == 1) {
                return current.step + 1;
            }

            if (bi + 1 <= 1000 && field[ai][bi + 1] == 0) {
                queue.addLast(new Point(ai, bi + 1, current.step + 1));
                field[ai][bi + 1] = -1;
            } else if (field[ai][bi + 1] == 1) {
                return current.step + 1;
            }
        }
        return 0;
    }
}

class Point {

    int a = 0;
    int b = 0;
    int step = 0;

    public Point(int a, int b, int step) {
        this.a = a;
        this.b = b;
        this.step = step;
    }
}
							
Posted in poj | Leave a comment

Poj Solution 3625

http://poj.org/problem?id=3625

 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;

 public class Main {

     public static void main(String[] args) throws IOException {
         BufferedReader read = new BufferedReader(new InputStreamReader(
                 System.in));
         String[] s = read.readLine().split(" ");
         int n = Integer.parseInt(s[0]);
         int m = Integer.parseInt(s[1]);
         int p1, p2;
         double[][] a = new double[n][n];
         int[][] p = new int[n][2];
         for (int i = 0; i < n; i++) {
             s = read.readLine().split(" ");
             p[i][0] = Integer.parseInt(s[0]);
             p[i][1] = Integer.parseInt(s[1]);
         }
         for (int i = 0; i < n; i++) {
             for (int j = i + 1; j < n; j++) {
                 a[i][j] = a[j][i] = Math.sqrt(Math.pow(p[i][0] - p[j][0], 2)
                         + Math.pow(p[i][1] - p[j][1], 2));
             }
         }
         for (int i = 0; i < m; i++) {
             s = read.readLine().split(" ");
             p1 = Integer.parseInt(s[0]);
             p2 = Integer.parseInt(s[1]);
             a[p1 - 1][p2 - 1] = a[p2 - 1][p1 - 1] = 0;
         }
         System.out.printf("%.2f", prim(a, n));
     }

     public static double prim(double[][] a, int count) {
         double sum = 0;
         int i, j, k;
         double[] lowcost = new double[count];
         double[] closeset = new double[count];
         boolean[] used = new boolean[count];
         for (i = 0; i < count; i++) {
             lowcost[i] = a[0][i];
             closeset[i] = 0;
             used[i] = false;
         }
         used[0] = true;
         for (i = 1; i < count; i++) {
             j = 0;
             while (used[j]) {
                 j++;
             }
             for (k = 0; k < count; k++) {
                 if ((!used[k]) && (lowcost[k] < lowcost[j])) {
                     j = k;
                 }
             }
             sum += lowcost[j];
             used[j] = true;
             for (k = 0; k < count; k++) {
                 if (!used[k] && (a[j][k] < lowcost[k])) {
                     {
                         lowcost[k] = a[j][k];
                         closeset[k] = j;
                     }
                 }
             }
         }
         return sum;
     } 
}

							
Posted in poj | Leave a comment

Poj Solution 3624

http://poj.org/problem?id=3624

//* @author: 82638882@163.com
import java.io.*;
public class Main
{
 static int[] p,q,d;
 static int a,b;
 public static void main(String[] args) throws IOException
 {
    InputStreamReader is=new InputStreamReader(System.in);
    BufferedReader in=new BufferedReader(is);
    String[] ss=in.readLine().split(" ");
    a=Integer.parseInt(ss[0]);
    b=Integer.parseInt(ss[1]);
    p=new int[a+1];
    q=new int[a+1];
    for(int i=1;i<=a;i++)
    {
         ss=in.readLine().split(" ");
      p[i]=Integer.parseInt(ss[0]);
      q[i]=Integer.parseInt(ss[1]);
    }
    d=new int[b+1];
    for(int i=1;i<=a;i++)
        for(int j=b;j>=p[i];j--)
            d[j]=Math.max(d[j-p[i]]+q[i], d[j]);
    System.out.println(d[b]);
  }
}
							
Posted in poj | Leave a comment

Poj Solution 3619

http://poj.org/problem?id=3619



import java.util.*;
import java.io.*;
/*
 * n ����;
 * s[i] ÿ���ӿ���ɵĹ���
 * t[i] һ����l��������ֵ�ʱ��
 * r[i] һ��l�������Ҫ��Ϣ��ʱ�� 
*/
public class Main{
    
    public static void main(String rgs[]) throws Exception
    {
        BufferedReader stdin = 
            new BufferedReader(
                new InputStreamReader(System.in));
           String line = stdin.readLine();
           StringTokenizer st = new StringTokenizer(line);
           int k,s,t,r,i,j,n,m,l,p;   
        n = Integer.parseInt(st.nextToken());
        k = Integer.parseInt(st.nextToken()); 
        for(i=0;i< k;i++){
            m=l=p=0;
            line = stdin.readLine();
               st = new StringTokenizer(line);
               s = Integer.parseInt(st.nextToken());
               t = Integer.parseInt(st.nextToken());
               r = Integer.parseInt(st.nextToken());
               while(l< n){
                   while(l< n && p< t){
                       l+=s;
                       m++;
                       p++;
                   }
                   if(l< n){
                       m+=r;
                       p=0;
                   }
               }
            System.out.println(m);    
        }    
    }
}

							
Posted in poj | Leave a comment

Poj Solution 3601

http://poj.org/problem?id=3601

//* @author popop0p0popo
import java.util.*;
import java.io.*;

public class Main{
    public static int step;
    public static int[] h;
    public static void main(String[] args){
     Scanner scanner=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int n,m;
        while (scanner.hasNext()){
            n=scanner.nextInt();
            m=scanner.nextInt();
            step=0;
            h=new int[n];
            for (int i=0;i< n ;i++ ){
                h[i]=scanner.nextInt();
            }
            step=getStep(h.length-1,m);
            System.out.println(step%m);
        }
    }

    public static int getStep(int moved,int m){
        if (moved==0){
            return 2*h[moved]-1;
        }
        int total=h[0];
        for (int i=1;i< moved ;i++ ){
            total=(total*2+h[i])%m;
        }
        if (h[moved]==1){
            return total*2+1;
        }
        else{
            return 2*total+2*h[moved]+getStep(moved-1,m);
        }
    }
}


							
Posted in poj | Leave a comment

Poj Solution 3589

http://poj.org/problem?id=3589

//* @author ������&lt;hongxp11@163.com&gt;
import java.util.Scanner;

    
public class Main {
 public static void main(String[] args)
 {
    Scanner in = new Scanner(System.in);
    int n = Integer.parseInt(in.nextLine());
    for(int j = 0; j< n; j++)
    {
        String first = in.next();
        String second = in.next();
        int res1 = 0;
        int res2 = 0;
        int[] done = new int[4];
        for(int i = 0; i< 4; i++)
        {
            if(first.charAt(i)==second.charAt(i))
            {    
                res1++;
                done[i]=1;
            }        
        }
        for(int i = 0; i< 4; i++)
        {
                for(int k = 0; k< 4; k++)
           {
            if(first.charAt(i)==second.charAt(k))
            {
                if(done[i]==0)
                    res2++;
            }
        }
    }
    System.out.println(res1+"A"+res2+"B");
  }
 }
}

							
Posted in poj | Leave a comment

Poj Solution 3536

http://poj.org/problem?id=3536

//* @author: 82638882@163.com
import java.util.*;
public class Main
{
 public static void main(String[] args)
 {
  Scanner in=new Scanner(System.in);
  while(in.hasNext())
  {
   int a=in.nextInt();
   int x=1,y=1,z=a,m=1,n=1,q=a;
   for(int i=(int)Math.pow(a,1.0/3)+1;i>0;i--)
   {
    if(a%i==0)
    {
         x=i;
      for(int j=(int)Math.sqrt(a/x)+1;j>0;j--)
      {
       if((a/x)%j==0)
       {
        y=j;
        z=a/x/j;
        if(x*y+y*z+z*x< m*n+n*q+q*m)
        {
        m=x;
        n=y;
        q=z;
        }
        break;
       }
      }
     }
     }
     System.out.println(m+" "+n+" "+q);
   }
 }
}
							
Posted in poj | Leave a comment

Poj Solution 3522

http://poj.org/problem?id=3522

//* @author: 
import java.io.*;
import java.util.*;
/*�����������Ȩֵ����СȨֵ����С��:
 *��˼��:�ȶԱ�����. ���������ڵ�һ����������,��¼���Ȩֵ����СȨֵ����С��,ö���������
 *��α�֤�������������Ž���,��Ϊ���Ѿ���Ȩֵ��������,���ڵ�һ�����ɵ����Ȼ����С��(�Ͻ���½�֮����С)
 */
class cin
{
static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
static int a,c;
static int nextInt() throws IOException
{
   c=in.read();
   a=0;
   while(c==' '||c=='r'||c=='n')c=in.read();
   while(c!=' '&&c!='r'&&c!='n')
   {
    a=a*10+c-'0';
    c=in.read();
   }
   return a;
}
}

class E
{
int weight,from,to;
void init(int x,int y,int z)
{
   weight=z;
   from=x;
   to=y;
}
}

class Cmp implements Comparator
{
public int compare(Object a,Object b)
{
   int c=((E)a).weight-((E)b).weight;
   if(c< 0)return -1;
   return 1;
}
}

class Set
{
int father[];
int num;
Set(int n)
{
   num=n;
   father=new int[n+1];
   this.clear();
}

void clear()
{
   int i;
   num=father.length-1;
   for(i=1;i<=num;i++)father[i]=i;
}

int findF(int x)
{
   int t=x;
   while(t!=father[t])
   {
    t=father[t];
   }
   while(x!=father[x])
   {
    father[x]=t;
    x=father[x];
   }
   return t;
}

boolean union(int x,int y)
{
   int fx=findF(x),fy=findF(y);
   if(fx==fy)return false;
   father[fy]=fx;
   num--;
   return true;
}
}

class Kruskal
{
E e[];
int numOfE,numOfTree,i,j,numOfD;
Set points;

Kruskal(E a[],int m,int n)
{
   e=a;
   numOfE=m;
   numOfD=n;
   points=new Set(numOfD);
}

int cost()
{
   int dis=100000,h,k=0;
   Arrays.sort(e,new Cmp());
   for(i=0;i< numOfE;i++)    //��Ҫ���ִ���
   {
    if(points.union(e[i].from,e[i].to))
    {
     if(points.num==1)   //���һ����
     {
      k++;
      while(true)
      {
       points.clear();
       for(h=k;h<=i;h++)
       {
        points.union(e[h].from, e[h].to);
       }
       if(points.num==1)k++; //ȥ���Ҫ�ı�
       else break;
      }
      if(dis>e[i].weight-e[k-1].weight) //���㲢������С��ֵ
       dis=e[i].weight-e[k-1].weight;
     }
    }
   }
   if(dis==100000)dis=-1;
   return dis; 
}

}

public class Main {
    public static void main(String args[]) throws IOException
    {
    int n,m,i;
    E e[];
    while(true)
    {
       n=cin.nextInt();
       m=cin.nextInt();
       if(n==0&&m==0)break;
       e=new E[m];
       for(i=0;i< m;i++)
       {
        e[i]=new E();
        e[i].init(cin.nextInt(), cin.nextInt(), cin.nextInt());
       }
       Kruskal data=new Kruskal(e,m,n);
       System.out.println(data.cost());
    }
    }
 }
							
Posted in poj | Leave a comment