ACM-数据结构

{% blockquote 本文出自 http://svtter.github.io svtter.github.io %}

本文可以随意转载,但是转载请保留本信息.

Uvaoj的判题效率不是很高。。所以直接开下一章节。题目慢慢刷,先过一遍书,不然书都看不完了TAT。。

6.1 栈和队列

卡片游戏,回顾了下队列和STL

#include <stdio.h>
#include <queue>
#include <iostream>
using namespace std;
const int maxn = 100;
int q[maxn];
void ace()
{
    void ace2(int);
    int n;
    scanf("%d", &n);
    //init
    for(int i = ; i < n; i++) q[i] = i+1;
    int front , rear;
    front = , rear = n;
    // 队列不为空
    while(front != rear)
    {
        printf("%d ", q[front++]);
        q[rear++] = q[front++];
    }
    printf("\n");
    ace2(n);
}
// STL 
void ace2(int n)
{
    queue <int> q;
    for(int i = 1; i <= n; i++) q.push(i);
    while(!q.empty())
    {
        printf("%d ", q.front());
        q.pop();
        q.push(q.front());
        q.pop();
    }
    printf("\n");
}
int main(int argc, const char *argv[])
{
    ace();
    return ;
}

6.1.2栈的STL

#include <stdio.h>
#include <iostream>
#include <stack>
using namespace std;
const int maxn = 200;
int s[maxn];
void ace()
{
    int n;
    int a[maxn];
    int temp, top;
    int p, set;
    while(~scanf("%d", &n))
    {
        top = -1, p = , set = 1;
        for(int i = ; i < n; i++) a[i] = i+1;
        for(int i = ; i < n; i++)
        {
            scanf("%d", &temp);
            // 如果无法完成序列,只吸收输入值
            if(!set) continue;
            if(temp == a[p]) p++;
            else if(top != -1 && temp == s[top]) top--;
            else while(temp != a[p])
                {
                    s[++top] = a[p];
                    p++;
                    if(p == n)
                    {
                        set = ;
                        break;
                    }
                }
        }
        if(set) printf("Yes");
        else printf("No");
        printf("\n");
    }
}
void ace2()
{
    int n;
    int a[maxn];
    int temp, p, set;
    while(~scanf("%d", &n))
    {
        stack <int> s;
        p = , set = 1;
        for(int i = ; i < n; i++) a[i] = i+1;
        for(int i = ; i < n; i++)
        {
            scanf("%d", &temp);
            // 如果无法完成序列,只吸收输入值
            if(!set) continue;
            if(temp == a[p]) p++;
            else if(!s.empty() && temp == s.top()) s.pop();
            else while(temp != a[p])
                {
                    s.push(a[p]);
                    p++;
                    if(p == n)
                    {
                        set = ;
                        break;
                    }
                }
        }
        if(set) printf("Yes");
        else printf("No");
        printf("\n");
    }
}
int main()
{
    ace2();
    return ;
}

6.2 链表和随机数发生器

链表的相关部分就不在赘述了。主要是随机数发生器。

很多人喜欢用rand()%N得到一个随即整数,但是n大于RAND_MAX的时候,就不好用了。

于是使用(double)rand()/RAND_MAX,然后在扩大n-1倍以后四舍五入,再+1

例如这样

// 获取1~m的随即整数
int random(int m)
{
    double a = (double)rand() / RAND_MAX;
    return (int) (a * (m-1) + 0.5);
}

6.3 二叉树

Java-Ant文件和Synastic-Vim支持

{% blockquote 本文出自 http://svtter.github.io svtter.github.io %}

本文可以随意转载,但是转载请保留本信息.

本文已经更新,请看 — 如何使用vim开发Java.

本来想把本文命名为使用Vim写Java-Project的,但是个人认为这样并没有很高的开发效率,所以只是提

供一个部分的解决方案,剩下的就由大家努力探索吧。。

Java有个自己构建工程的工具叫做Ant, 全名Apache Ant,好东西,最通俗的解释就是Java的

makefile,这里提供的方案,就是使用Ant或者maven来构建工程,然后通过Synastic的设置,来完成

Java的代码检查。另外,java代码补全设置其实也是有的,但是具体的设置我没有好好研究过,主要是

已经陪不起那个时间了。

另外,Eclipse导出Java项目的时候,一个不错的方法就是Export Ant Buildfiles,这就是ant文件。

Maven我没有好好学,但是看wiki似乎Synastic对maven的支持比ant的支持更加优秀,也可能是mvn的构

建方法上存在优势?这个我不清楚。有时间写一篇文章来好好的研究下。

Ant这个东西构建的包有时候检查会出现问题,(如果采用目录编译的方法,那么就无法检查)。

另外,需要不停的在目录下建立.synastic_javac_config,着实烦人。但是如果使用了全局设置,改

变了配置文件的根目录,也是极为不妥当的(不可能只有一个项目在编辑吧?另外改来改去,也是太麻

烦了。)

就是这样。。更多的是吐槽吧。

对于上一篇文章提到的问题可以用设置classpath来解决。具体方法是将classpath添加到syntastic。

use :SyntasticJavacEditClasspath and add workpath.

Java  ant  vim 

Java-import和package的用法

本文出自svtter.github.io 本文可以随意转载,但是转载请保留本信息.

之前一直都是用eclipse开发Java,Vim修炼到今日,本来想要拉出来溜溜Java,奈何package和import的机制以及 synastic[自动检查代码的插件] 没有对于eclipse对java包类似的支持,所以只得滚回eclipse,然后使用vrapper了。

[阅读全文]

php-测试表单的php文件

用于测试form, 上传的文件

<?php
// 乱码问题
// 页面设置为gb2312, 如果是utf-8, 则改为uft-8
header("Content-type: text/html; charset=gb2312");
// 针对ajax, 
// iconv( "UTF-8", "gb2312" , $_POST["post"]);
//用于测试submit, 显示所有上传的变量
function test_submit()
{
    while(list($key,$val) = each($_POST))
    {
        echo "$key => $val<br />";
    }
}
test_submit();
//保存到固定文件,重新转存
function one_file_upload($name)
{
    // 测试单文件
    if ($_FILES[$name]["error"] > )
    {
        echo "Error: " . $_FILES[$name]["error"] . "<br />";
    }
    else
    {
        echo "Upload: " . $_FILES[$name]["name"] . "<br />";
        echo "Type: " . $_FILES[$name]["type"] . "<br />";
        echo "Size: " . ($_FILES[$name]["size"] / 1024) . " Kb<br />";
        echo "Stored in: " . $_FILES[$name]["tmp_name"];
    }
}
// one_file("file");
//  save in a array
function reArrayFiles(&$file_post) {
    //  用于规格化输入的字符
    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);
    for ($i=; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }
    return $file_ary;
}
// 显示上传的文件
function show($name) {
    if ($_FILES[$name]['size']) {
        $file_ary = reArrayFiles($_FILES[$name]);
        foreach ($file_ary as $file) {
            print 'File Name: ' . $file['name'] . '<br/>';
            print 'File Type: ' . $file['type'] . '<br/>';
            print 'File Size: ' . $file['size'] . '<br/>';
            print '<br/>';
        }
    }
    else {
        echo "Files upload == 0" . "<br/>";
    }
}
show('userpic');
show('userfile');
// 测试服务器
function test_server() {
    while( list($key,$val) = each($_SERVER))
    {
        echo "$key=> $val<br />";
    }
}
?>
php 

JS-变量相关-jQuery调用

  • 本文出自<svtter.github.io>

在javascript函数外部定义的变量均为全局变量。

调用jQuery, 需要把jQuery放在<script>最前。

调用本地jQuery的代码

jQuery 目录: ../js/jquery-1.11.1.min.js/

其他的script文件放在jquery后即可

HTML

依据w3cschool修改

<html>
<head>
<script type="text/javascript" src="../js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    alert("debug!")
  $("button").click(function(){
    $("p").hide();
  });
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>

JS-优化上传文件的样式

上传文件type=file真心丑,所以采用方法解决一下。

中心思想是隐藏input,然后利用css定位,使其仍然可以被点击(有点欺骗的性质)

text需要禁止点击(使用diabled='disabled')

源代码

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>定义input type="file" 的样式</title>
        <style type="text/css">
body{ font-size:14px;}
input{ vertical-align:middle; margin:; padding:}
.file-box{ position:relative;width:340px}
.txt{ height:22px; border:1px solid #cdcdcd; width:180px;}
.btn{ background-color:#FFF; border:1px solid #CDCDCD;height:24px; width:70px;}
.file{ position:absolute; top:; right:85px; height:24px; filter:alpha(opacity:);opacity: ;width:70px }
        </style>
    </head>
    <body>
        <div class="file-box">
            <form action="" method="post" enctype="multipart/form-data">
                <input type='text' name='textfield' id='textfield' class='txt' disabled='disabled'/>
                <input type='button' class='btn' value='浏览...' />
                <input type="file" name="fileField" class="file" id="fileField" size="28" onchange="document.getElementById('textfield').value=this.value" />
                <input type="submit" name="submit" class="btn" value="上传" />
            </form>
        </div>
    </body>
</html>

C++学习必备


  • 本文出自<svtter.github.io>

  • (cplusplus)[http://www.cplusplus.com]

介绍三个头文件

在/usr/include/stdint.h中包含着各种关于C数据类型的定义,大小等。

  • unistd.h unix standard library header 这个函数库中包含read, write, gitpid等函数
  • stdlib.h standard library header
cpp 

php-php的类

关于PHP的类 http://php.net/manual/zh/language.oop5.php

(本来想顺便学学nodejs的,但是谁有那么多精力啊。。还有js学的像狗屎一样= =)

<?php
class Test
{
    private $name; // 可以定义类的访问权限
    public function __construct($name) //构造函数
    {
        $this->name = $name;
    }
    public function echoname
    {
        echo $this->name;
    }
}
$a = new Test("This is a.");
$a->echoname();
  • 构造函数重载我不会,有时间再更新吧。。
  • 先记录这些。
php 

n种方法实现并行枚举排序

这篇文章一直没有写,因为并行计算的报告写的比较潦草。此外,没有实现fork。

文件的源代码贴在 https://github.com/Svtter/workspace/tree/master/parallel/enum_sort

实现了Java, MPI, openmp, pthread, win32, MFC, .NET 的并行枚举排序,测试机是双核四线程的ThinkpadE430.

MPI的环境是archlinux . openmpi

贴一个MPI的源代码, 运行结果都在源代码对应的文件夹中保存,这里就不贴了。

/*=============================================================================
#
# Author: svtter - [email protected]
#
# QQ : 57180160
#
# Last modified: 2014-11-02 17:08
#
# Filename: enum_sort_MPI.cpp
#
# Description: 
#
=============================================================================*/
#include "mpi.h"
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <iostream>
using namespace std;
#define MAXN 10000
#define PMAX 1000
void build(int a[], int b[])
{
    srand(time(NULL));
    for(int i = 1; i <= MAXN; i++)
        a[i] = b[i] = random()%PMAX;
}
//serial enum sort
double serial_enum_sort(int a[], int at[])
{
    double t1, t2;
    t1 = MPI_Wtime();
    int k, i, j;
    for(i = 1; i <= MAXN; i++)
    {
        k = 1;
        for(j = 1; j <= MAXN; j++)
            if(a[i] > a[j] || (a[i] == a[j] && i > j))
                k++;
        at[k] = a[i];
    }
    t2 = MPI_Wtime();
    return (t2 - t1);
}
// 用于调试数组
void debug(int a[], int len)
{
    for(int i = 1; i <= len; i++)
    {
        fprintf(stderr, "%5d", a[i]);
    }
    fprintf(stderr, "\n");
}
int a[MAXN+10], b[MAXN+10], at[MAXN+10], bt[MAXN+10];
void ensort(int rank[], int &myid, int &numprocs)
{
    int i, j, k;
    for(i = myid; i <= MAXN; i+=numprocs)
    {
        k = 1;
        for(j = 1; j <= MAXN; j++)
        {
            if(b[i] > b[j] || (b[i] == b[j] && (i > j)))
                k++;
        }
        rank[i] = k;
    }
}
int main(int argc, char *argv[])
{
    int myid, numprocs;
    int namelen;
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    double c1 = , c2;
    double start, end;
    int i;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &myid);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    MPI_Get_processor_name(processor_name, &namelen);
    // fprintf(stderr, "Hello world! Processor %d of %d on %s\n", myid, numprocs, processor_name);
    //serial
    if(myid == )
    {
        build(a, b);
        c1 = serial_enum_sort(a, at);
        cout << "original array is: " << endl;
        debug(a, 100);
        cout << endl;
        cout << "serial sorted array is: " << endl;
        debug(at, 100);
        cout << endl;
        cout << "serial cost time is: " << c1 << endl;
        cout << endl;
    }
    int con[numprocs][MAXN+10];
    int pt[MAXN+10];
    memset(con ,, sizeof(con));
    memset(pt,, sizeof(pt));
    // int **con = new int*[numprocs];
    // for(int i = 0; i < numprocs; i++)
        // con[i] = new int[MAXN+10];
    start = MPI_Wtime();
    // P0 send b to ALL
    MPI_Bcast(b, MAXN+10, MPI_INT, , MPI_COMM_WORLD);
    ensort(pt, myid, numprocs);
    // Gather
    MPI_Gather(pt, MAXN+10, MPI_INT, con[myid], MAXN+10, MPI_INT, , MPI_COMM_WORLD);
    // if(myid == 0)
    // {
        // fprintf(stderr, "myid: %d\n", myid);
        // fprintf(stderr, "con: %d\n", myid);
        // debug(con[1], 100);
        // fprintf(stderr, "pt: %d\n", myid);
        // debug(pt, 100);
        // fprintf(stderr, "\n");
    // }
    if(myid == )
    {
        int j;
        // for(i = 0; i < numprocs; i++)
        // {
            // printf("i: %d\n", i);
            // for(j = 1; j <= MAXN; j++)
                // printf("%5d", con[i][j]);
            // puts("");
        // }
        // rank[k] = i
        for(i = ; i < numprocs; i++)
            for(j = 1; j <= MAXN; j++)
                bt[con[i][j]] = b[j];
        // fprintf(stderr, "bt: \n");
        cout << "parallel sorted array is: " << endl;
        debug(bt, 100);
        cout << endl;
        end = MPI_Wtime();
        c2 = end - start;
        fprintf(stderr, "parallel cost time is: %lf\n", c2);
        fprintf(stderr, "加速比为: %lf\n", c1 / c2);
    }
    // for(i = 0; i < numprocs; i++)
        // delete con[i];
    // delete con;
    MPI_Finalize();
    return ;
}

并行计算非常有趣,有时间肯定会再在这条道路上探寻…

[阅读全文]