Friday, April 29, 2016

【國際象棋】國象--開局(I)[白方]


有時候身為黑方時,用作為白方時的套路,卻不能達到白方時的效果,這是為什麼呢?

而要怎樣做才能走出對自己更有利的局面呢?

一般對白方來說,e4是最常見的第一步



而這時候黑方會怎樣應對呢?

對稱防禦:1. e4 e5 2.Kf3


黑方這樣做確實可以阻止白方的前進,然而白方的kf3馬上就可以吃死對方了,這時候黑方可以走Kc6來防止白方的進攻,但這樣做真的做成更有利的局面嗎?

不對稱防禦:1. e4 c5


與對稱法相反的動作,做出了使局勢平衡的效果


斯堪的納維亞防禦:1.e4 d5



用同樣的走法來應對白方,而白方是否吃掉黑棋會對之後的棋局做成影響

法式防禦: 1. e4 e6


Caro-Kann防禦: 1.e4 c6


 Pirc防禦: 1.e4 d6 2.d4 Nf6 3.Nc3 g6


——-所有圖片裁自http://zh.lichess.org/editor

Thursday, April 28, 2016

【國際象棋】--Start

診le段時間好好甘研究下棋譜~


圖片來自Lev Alburt 的 Chess Openings for White, Explained

這算是國際象棋的基礎吧

記錄方法:

國王--K

皇后--Q

車/城堡--R

馬/騎士--N

主教--B

默認不寫就是 卒

格式:

Nc3 //馬移動到e5

a3 //卒移動到a3

axb5 //a4的卒吃掉b5的棋子

Bb5xc6 或 Bxc6 //c5的主教吃掉c6的棋子

當有兩只棋子可以走到同一格時,就要寫出該棋的起始格

e.g. Rad3 //a3的車移動到d3

Wednesday, April 27, 2016

【CodeForces】Registration system

problem here

Problem

A new e-mail service “Berlandesk” is going to be opened in Berland in the near future. The site administration wants to launch their project as soon as possible, that’s why they ask you to help. You’re suggested to implement the prototype of site registration system. The system should work on the following principle.
Each time a new user wants to register, he sends to the system a request with his name. If such a name does not exist in the system database, it is inserted into the database, and the user gets the response OK, confirming the successful registration. If the name already exists in the system database, the system makes up a new user name, sends it to the user as a prompt and also inserts the prompt into the database. The new name is formed by the following rule. Numbers, starting with 1, are appended one after another to name (name1, name2, …), among these numbers the least i is found so that namei does not yet exist in the database.

INPUT

The first line contains number n (1 ≤ n ≤ 105). The following n lines contain the requests to the system. Each request is a non-empty line, and consists of not more than 32 characters, which are all lowercase Latin letters.

OUTPUT

Print n lines, which are system responses to the requests: OK in case of successful registration, or a prompt with a new name, if the requested name is already taken.

Sample

input


abacaba 
acaba 
abacaba 
acab

output

OK 
OK 
abacaba1 
OK

input


first 
first 
second 
second 
third 
third

output

OK 
first1 
OK 
second1 
OK 
third1

Solution

hash table
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <utility>
using namespace std;

int main(){
    int n;
    while(cin >> n){
        unordered_map<string, int> um;
        while(n--){
            string input;
            cin >> input;
            unordered_map<string, int>::const_iterator it = um.find(input);
            if(it == um.end()){
                um.insert(pair<string, int>(input, 1));
                cout << "OK" << endl;
            }else{
               stringstream ss;
               int num = it->second;
               ss << num;
               string str;
               ss >> str;
               string key = it->first;
               string data = key + str;
               um.erase(it->first);
               um.insert(pair<string, int>(key, ++num));
               cout << data << endl;
            }
        }
    }

    return 0;
}

Sunday, April 24, 2016

【TopCoder】 SRM 144 div 2 200

Problem

Problem Statement
Computers tend to store dates and times as single numbers which represent the number of seconds or milliseconds since a particular date. Your task in this problem is to write a method whatTime, which takes an int, seconds, representing the number of seconds since midnight on some day, and returns a string formatted as “< H >:< M >:< S >”. Here, < H > represents the number of complete hours since midnight, < M > represents the number of complete minutes since the last complete hour ended, and < S > represents the number of seconds since the last complete minute ended. Each of < H >, < M >, and < S > should be an integer, with no extra leading 0’s. Thus, if seconds is 0, you should return “0:0:0”, while if seconds is 3661, you should return “1:1:1”. 
Definition

Class:

Time

Method:

whatTime

Parameters:

int

Returns:

string

Method signature:

string whatTime(int seconds) 
(be sure your method is public)

Limits

Time limit (s):

2.000

Memory limit (MB):

64

Constraints

seconds will be between 0 and 24*60*60 - 1 = 86399, inclusive.

Examples

0)

Returns: “0:0:0”
1)
3661 
Returns: “1:1:1”
2)
5436 
Returns: “1:30:36”
3)
86399 
Returns: “23:59:59”
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

Solution

Get 155.57/200 points
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class Time{
    public:
        string whatTime(int seconds);

};

string Time::whatTime(int seconds){
    int h = seconds / 60 / 60;
    int m = seconds / 60 - h*60;
    int s = seconds - h*60*60 - m*60;
    stringstream ss;
    string str, str2, str3;
    ss << h;
    ss >> str;
    ss.clear();
    ss << m;
    ss >> str2;
    ss.clear();
    ss << s;
    ss >> str3;
    return str + ":" + str2 + ":" + str3;
}

Monday, April 18, 2016

【UVa】495 - Fibonacci Freeze

Problem here

Problem

The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …) are defined by the recurrence:
eqnarray20
Write a program to calculate the Fibonacci Numbers.

INPUT&OUTPUT

The input to your program would be a sequence of numbers smaller or equal than 5000, each on a separate line, specifying which Fibonacci number to calculate.
Your program should output the Fibonacci number for each input value, one per line.

Sample

input



11

output

The Fibonacci number for 5 is 5 
The Fibonacci number for 7 is 13 
The Fibonacci number for 11 is 89

Solution

這題要用大數加法才不會WA
#include <iostream>
using namespace std;
int f[5001][5001] = {0};

int main(){
    f[1][0] = 1;
    for(int i = 2; i <= 5000; i++){
        for(int j = 0; j < 5001; j++){
            f[i][j] += f[i-1][j] + f[i-2][j];
            f[i][j+1] += f[i][j]/10;
            f[i][j] %= 10;
        }
    }
    int n;
    while(cin >> n){
        cout << "The Fibonacci number for " << n << " is "; 
        int i;
        for( i = 5000 ; i >= 0 ; i-- )
            if( f[n][i] )
             break;
        if( i == -1 )
          cout << 0;
        else
          for( ; i >= 0 ; i-- )
            cout << f[n][i];
        cout << endl;
    }
    return 0;
}

Sunday, April 17, 2016

【LeetCode】70. Climbing Stairs

Problem here

Problem

You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Solution

這題之前做過 
就是超級樓梯那條
class Solution {
public:
    int climbStairs(int n) {
        vector<int> f;
        f.push_back(1);
        f.push_back(1);
        for(int i = 2; i <= n; i++){
            f.push_back(f[i-1] + f[i-2]);
        }
        return f[n];

    }
};

Saturday, April 16, 2016

【NEFU】17-数字三角形

Problem here

Problem


3 8 
8 1 0 
2 7 4 4 
4 5 2 6 5 
给出了一个数字三角形。从三角形的顶部到底部有很多条不同的路径。对于每条路径,把路径上面的数加起来可以得到一个和,你的任务就是找到最大的和。 
注意:路径上的每一步只能从一个数走到下一层上和它最近的左边的那个数或者右边的那个数。

INPUT

输入数据有多组,每组输入的是一行是一个整数N (1 < N <= 100),给出三角形的行数。下面的N行给出数字三角形。数字三角形上的数的范围都在0和100之间。

OUTPUT

输出最大的和。

Sample

input



3 8 
8 1 0 
2 7 4 4 
4 5 2 6 5 


2 3 
1 1 1

output

30 
5

Solution


3 8 
8 1 0 
2 7 4 4 
4 5 2 6 5 
以這個為例 
設x,y為位置 
則f(x,y)為到了(x,y)時的路徑長度 
所以可以得知 
f(1,1) = 7 <—第一個數嘛 
f(1+1, 1)=f(1,1)+f(1+1,1)=10 
f(1+1,1+1)=f(1,1)+f(1+1,1+1)=15 
f(3,1)= f(2,1)+f(3,1)=18 
f(3,2)=max(10,15)+1=16 
由此可知 
f(x,y)=max(f(x-1,y), f(x-1,y-1))+f(x,y)
#include <iostream>
#include <algorithm>
#include <memory.h>
using namespace std;

int main(){
    int n;
    while(cin >> n){
        int tg[101][101];
        memset(tg, 0, sizeof(tg));
        cin >> tg[1][1];
        for(int i = 2; i <= n; i++){
            for(int j = 1; j <= i; j++){
                cin >> tg[i][j];
                tg[i][j] = max(tg[i-1][j], tg[i-1][j-1]) + tg[i][j];
            }
        }
        int max = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(max < tg[i][j]){
                    max = tg[i][j];
                }
            }
        }
        cout << max << endl;
    }
    return 0;
}

Installing xAct (Mac)

Download from http : www.xact.es . Before unpack the tar/tgz file, remove attributes with xattr -c xAct_1.x.x.tgz then place the un...