1106. Lowest Price in Supply Chain (25)

1.该题是供应链问题,实则是构建一棵树,然后使用BFS进行层序遍历,找出最早出现叶子节点的层,统计该层的叶子节点,然后直接输出结果。

1106

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone involved in moving a product from supplier to customer.

Starting from one root supplier, everyone on the chain buys products from one’s supplier in a price P and sell or distribute them in a price that is r% higher than P. Only the retailers will face the customers. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

Now given a supply chain, you are supposed to tell the lowest price a customer can expect from some retailers.

Input Specification:

Each input file contains one test case. For each case, The first line contains three positive numbers: N (<=105), the total number of the members in the supply chain (and hence their ID’s are numbered from 0 to N-1, and the root supplier’s ID is 0); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then N lines follow, each describes a distributor or retailer in the following format:

Ki ID[1] ID[2] … ID[Ki]

where in the i-th line, Ki is the total number of distributors or retailers who receive products from supplier i, and is then followed by the ID’s of these distributors or retailers. Kj being 0 means that the j-th member is a retailer. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the lowest price we can expect from some retailers, accurate up to 4 decimal places, and the number of retailers that sell at the lowest price. There must be one space between the two numbers. It is guaranteed that the all the prices will not exceed 1010.

Sample Input:

10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0
2 6 1
1 8
0
0
0

Sample Output:

1.8362 2

AC代码:
[c language=”++”]
//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
bool cmp(const int&a, const int&b)
{
return a > b;
}
struct ChainNode{
vector<int> list;
ChainNode() :list(0){};
};
int main(void)
{
int total;
double rootPrice, higher;
scanf("%d %lf %lf", &total, &rootPrice, &higher);
vector<ChainNode> chain(total);

for (int i = 0; i < total; i++)
{
int nextTotal;//下游数量
scanf("%d", &nextTotal);
for (int j = 0; j < nextTotal; j++)
{
int tmp;
scanf("%d", &tmp);
chain[i].list.push_back(tmp);//记录下游节点
}
}

queue<int> q;
q.push(0);
int count1 = 1;
int count2 = 0;
int retailerSum = 0;//该层零售商数量
while (!q.empty())
{
for (int i = 0; i < count1; i++)
{
int head = q.front();
q.pop();
if (chain[head].list.size() == 0)
retailerSum++;//如果出现子节点,则进行计数记录
else
{//否则压入队列,下次遍历
for (int j = 0; j < chain[head].list.size(); j++)
{
count2++;
q.push(chain[head].list[j]);
}
}
}
//如果存在叶子节点,直接跳出
if (retailerSum != 0) break;
count1 = count2;
count2 = 0;
rootPrice *= (1 + higher / 100.0);
}
printf("%.4lf %d\n", rootPrice, retailerSum);

return 0;
}

[/c]

1105. Spiral Matrix (25)

1.回旋矩阵,主要是给出一个数组,先对数组进行降序排序,使用sort和重写比较函数,然后填充到回旋矩阵中。

2.回旋矩阵填充思路:先建立一个m*n的所有值为-1的矩阵,-1表示数值没被填充,

(1)一开始往右填充,当右边到达边界值或者右边的数已经被填充,就改为往下填充;

(2)往下填充遇到边界值或者下边的数已经被填充,则往左填充;

(3)往左填充遇到边界值或者左边的数已经被填充,改为往上走;

(4)往上填充遇到边界值或者上边的数已经被填充,则往右填充,回到(1)。

1105

 

时间限制
150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m rows and n columns, where m and n satisfy the following: m*n must be equal to N; m>=n; and mn is the minimum of all the possible values.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N. Then the next line contains N positive integers to be filled into the spiral matrix. All the numbers are no more than 104. The numbers in a line are separated by spaces.

Output Specification:

For each test case, output the resulting matrix in m lines, each contains n numbers. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.

Sample Input:

12
37 76 20 98 76 42 53 95 60 81 58 93

Sample Output:

98 95 93
42 37 81
53 20 76
58 60 76

AC代码:
[c language=”++”]
//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
bool cmp(const int&a, const int&b)
{
return a > b;
}
int main(void)
{
int total;
scanf("%d", &total);
vector<int> num(total, 0);
for (int i = 0; i < total; i++)
{//读取数据
scanf("%d", &num[i]);
}
//进行排序
sort(num.begin(), num.end(), cmp);
int matM, matN;
int minDiff = INT_MAX;
//求出m>=n且abs(m-n)最小时,m与n的取值
for (int i = 1; i <= sqrt((double)total) + 1; i++)
{
if (i*(total / i) == total&&abs(i – (total / i)) < minDiff)
{//如果符合要求,且差比上次的小,则进行更新
matM = i;
matN = total / i;
minDiff = abs(matM – matN);
}
}
if (matM < matN)
swap(matM, matN);

//读取的都是正数,利用-1来表示还没填充
vector<vector<int>>mat(matM, vector<int>(matN, -1));
int direction = 0;//方向,0表示往右,1表示往下,2表示往左,3表示往上
int m = 0, n = 0;//m,n表示上次填充的位置
for (int i = 0; i < total; i++)
{
mat[m][n] = num[i];
if (direction == 0)
{//往右走
if (n == matN – 1 || mat[m][n + 1] != -1)
{//如果遇到边界,或者右边的位置已经填充数值,则往下走
direction = 1;
m++;
}
else//继续往右走
n++;
}
else if (direction == 1)
{//往下走
if (m == matM – 1 || mat[m + 1][n] != -1)
{//如果遇到边界,或者下边的位置已经填充数值,则往左走
direction = 2;
n–;
}
else//继续往下走
m++;
}
else if (direction == 2)
{//往左走
if (n == 0 || mat[m][n – 1] != -1)
{//如果遇到边界,或者左边的位置已经填充数值,则往上走
direction = 3;
m–;
}
else//继续往左走
n–;
}
else if (direction == 3)
{//往上走
if (m == 0 || mat[m – 1][n] != -1)
{//如果遇到边界,或者上边的位置已经填充数值,则往右走
direction = 0;
n++;
}
else//继续往上走
m–;
}
}

for (int i = 0; i < matM; i++)
{
for (int j = 0; j < matN; j++)
{
printf("%d", mat[i][j]);
if (j != matN – 1)
printf(" ");
}
printf("\n");
}

return 0;
}

[/c]

1104. Sum of Number Segments (20)

1.题目要求一个序列中的所有连续子序列和的和。如下:对于序列{0.1, 0.2, 0.3, 0.4},有10个连续子序列: (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0.4).

2.直接进行暴力计算会超时,需要寻找出出现次数的数学关系。

3.关系如下,数组a[0],a[1],a[2]….a[n-1]共n个数,假设数字a[0]出现的次数times,对即a[0],a[0]a[1],a[0]a[1]a[2]…..不难看出出现了n次,除去包含a[0]序列,我们可以看出a[1]出现的次数为n-1次,然后我们再观察包含a[0]的序列中,a[1]出现了times-1次,那么a[1]出现的次数为times1=times-1+n-1。同样,把序列分为包含a[1]和不包含a[1]d两种情况分析,a[2]出现的次数为times2=times1-2+n-2,以此类推:a[3]出现的次数为times3=times2-3+n-3。

所以转化公式为a[i]出现的次数times[i]=times[i-1]-i+n-i。

通过上述公式,可以在O(n)的时间复杂度内计算出结果。

1104

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CAO, Peng

Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence {0.1, 0.2, 0.3, 0.4}, we have 10 segments: (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0.4).

Now given a sequence, you are supposed to find the sum of all the numbers in all the segments. For the previous example, the sum of all the 10 segments is 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N, the size of the sequence which is no more than 105. The next line contains N positive numbers in the sequence, each no more than 1.0, separated by a space.

Output Specification:

For each test case, print in one line the sum of all the numbers in all the segments, accurate up to 2 decimal places.

Sample Input:

4
0.1 0.2 0.3 0.4 

Sample Output:

5.00

AC代码:
[c language=”++”]
//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;

int main(void)
{
int n;
scanf("%d", &n);
vector<double> num(n, 0);
for (int i = 0; i < n; i++)
{
scanf("%lf", &num[i]);
}
double ans = 0;
long long times = 0;//需要使用long long,避免溢出
for (int i = 0; i < n; i++)
{//当前数字出现的次数与上一个数字出现的次数有联系,联系为times = times – i + n – i;
times = times – i + n – i;
ans += times*num[i];
}
printf("%.2lf\n", ans);
return 0;
}

[/c]

PAT甲级(Advance Level)冬季考试总结20151205

今天中午刚考完PAT甲级(Advance Level)冬季考试(20151205),4道题目分别考察的内容如下:

1.数学统计,题目给出了sequence的定义,根据该定义进行统计,通过定义我们可以找出每个数出现的次数规律,譬如有数组a[0],a[1],a[2]….a[n-1]共n个数,假设数字a[0]出现的次数times,那么a[1]出现的次数为times1=times-1+n-1,a[2]出现的次数为times2=times1-2+n-2,a[3]出现的次数为times3=times2-3+n-3,如此类推,最后可以在O(n)的时间内计算出结果。暴力解法需要O(n^3)的时间复杂度。

2.回旋矩阵,主要是给出一个数组,先对数组进行降序排序,使用sort和重写比较函数,然后填充到回旋矩阵中,回旋矩阵填充思路:先建立一个m*n的所有值为-1的矩阵,-1表示数值没被填充,(1)一开始往右填充,当右边到达边界值或者右边的数已经被填充,就改为往下填充;(2)往下填充遇到边界值或者下边的数已经被填充,则往左填充;(3)往左填充遇到边界值或者左边的数已经被填充,改为往上走;(4)往上填充遇到边界值或者上边的数已经被填充,则往右填充,回到(1)。

3.关于供应链问题,实则是建立一棵树,进行层次遍历,找到最早出现叶子节点的那一层,然后统计该层的叶子节点数量。实则是BFS,之前的练习题中出现了两道关于供应链的题目,所以比较容易理解。

4.并查集的考察。题目给出一些学生和该学生相应的兴趣,其中学生之间有两个共同的兴趣,即可归为一个cluster,不必所有兴趣都相同。所以采用并查集,把学生进行归类,最后进行统计。

题目尚未出现比较难的临界值,通过练习熟练掌握sort、BFS、并查集知识后可以较为轻松地应对这次考试。平时需要多练习,多手写代码,多整理自己的解题思路。后续练习题中出现本次的题目后,我再进行详细的解释和上代码。

PAT

关于博客访问速度慢或者不响应的原因

1.之前在本机一直没有发现博客打开有问题,但是在手机端或者别的电脑打开就会体会到速度非常慢,当时还没了解到是什么原因。

2.今天在另外一台电脑上面用火狐登录的时候发现显示正在解析fonts.googleapis.com的网站,然后想起会不会是加载了google的字体文件导致慢?因为google很多网址都被墙了,恰好我的电脑一直有翻墙,所以没有体会到。后来实际实验,这个网址没有被墙,但是通过查看Network,它竟然请求了200多s!

3.于是我通过F12的调试器查看Resources选项,发现加载了很多来自fonts.gstatic.com网站的font文件,这个网站实际上会解析到gstaticadssl-china.l.google.com。

字体问题

 

google

4.解决办法:找到wp-content\themes\XXXX(XXXX为主题名)中相应的functions.php文件,把其中的fonts.googleapis.com改为fonts.useso.com,把wp-includes\script-loader.php中的googleapis或者gstatics全部改为useso。

google2

 

google3google4

 

关于centOS与MacOS的locate命令

这两个系统在第一次这行locate命令时,都会出现找不到db的问题。

1.centOS

出现如下的问题:

[shell]

# locate test
locate: can not stat () `/var/lib/mlocate/mlocate.db’: No such file or directory

[/shell]

这个时候可以执行sudo updatedb进行建立索引数据库;

2.MacOS

可以根据提示进行操作,输入sudo /usr/libexec/locate.updatedb即可,因为要建立索引数据库,需要等待一段时间才能执行locate命令。

 

GitHub第二步:进行同步和提交

1.通过GitHub第一步:生成ssh keys,我们成功通过SSH连接github,接下来,我们介绍如何进行同步和提交。

2.同步github到本地,有下面的命令:

[shell]

git clone git://github.com:xxxx/test.git ##以gitreadonly方式克隆到本地,只可以读
git clone git@github.com:xxx/test.git ##以SSH方式克隆到本地,可以读写
git clone https://github.com/xxx/test.git ##以https方式克隆到本地,可以读写
git fetch git@github.com:xxx/xxx.git ##获取到本地但不合并
git pull git@github.com:xxx/xxx.git ##获取并合并内容到本地

[/shell]

3.本地提交项目到github,首先我们需要进行配置:

其中,为了各个帐号保持一致,即在github的profile里面有contributions记录,这里设置的user.email要与github帐号的email一致,否则不会有contributions记录,每台电脑上面的user.name可以不一样,email一致即可。

[shell]

git config –global user.name ‘siukwan’
git config –global user.email ‘siukwan@foxmail.com’ #全局联系方式,可选

[/shell]

然后进行add,commit

[shell]

git init #初始化一个本地库
git add testfile.txt#添加文件到本地仓库
git commit -m "first commit" #提交到本地库并备注,此时变更仍在本地。
git commit -a ##自动更新变化的文件,a可以理解为auto,注意需要添加注释,在:wq保存退出

[/shell]

这个时候,只是commit了本地的文件,还未把本地更新变更到github服务器上。通过下面的命令可以把更新推送到服务器上:

[shell]

git push #将本地文件提交到Github的remoname版本库中。此时才更新了本地变更到github服务上。

[/shell]

 

在进入到github的branch目录里面,可以直接进行git pull和git push操作。

注意:

如果在每次push的时候都需要输入帐号和密码,是因为使用了https方式 push(这个可以使用svn)

在终端里面输入

[shell]

git remote -v

[/shell]

可以看到下面的结果

[shell]

origin https://github.com/com:siukwan/unix (fetch)

origin https://github.com/com:siukwan/unix (push)

[/shell]

下面把它换成ssh方式的:

[shell]
git remote rm origin
git remote add origin git@github.com:siukwan/unix
git push origin
[/shell]

这时我们再次输入查看信息:

[shell]

git remote -v

[/shell]

可以看到下面的结果,这样在下次push的时候就不再需要输入帐号密码了。

[shell]

origin git@github.com:siukwan/unix (fetch)

origin git@github.com:siukwan/unix (push)

[/shell]

把当前目录添加到github仓库:
[shell]

git init

git push -u origin master

[/shell]

GitHub第一步:生成ssh keys

参考自https://help.github.com/articles/generating-ssh-keys/

亲自在Mac OS和cent OS中测试成功,命令均一样。

SSH keys可以无需密码进行github更新,每台电脑均需要生成唯一的SSH并添加到github个人资料的settings中。

centOS需要使用命令sudo yum install git安装git。

Step 1: 检查SSH keys

首先我们需要检测系统是否已经存在SSH keys,可以使用终端输入下列的命令:

ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist

如果存在,可以看到如下的列表:

  • id_dsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub
  • id_rsa.pub

如果想使用现有的SSH keys,可以忽略步骤2和步骤3,如果没有现成的SSH keys,我们接下来会通过终端来生成。

Step 2: 生成SSH key

  1. 输入下面的命令,其中需要填写自己的邮箱:
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    # Creates a new ssh key, using the provided email as a label
    # Generating public/private rsa key pair.
    
  2. 使用默认设置即可,直接按回车:
    # Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
    
  3. 这个也可以直接按回车:
    # Enter passphrase (empty for no passphrase): [Type a passphrase]
    # Enter same passphrase again: [Type passphrase again]
  4. 最后我们会看到下面的信息:
    # Your identification has been saved in /Users/you/.ssh/id_rsa.
    # Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
    # The key fingerprint is:
    # 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com
    

Step 3: 把生成的key添加到ssh-agent

ssh-agent是一种控制用来保存公钥身份验证所使用的私钥的程序,其实ssh-agent就是一个密钥管理器,原型ssh-agent以后,使用ssh-add将私钥交给ssh-agent保管,其他程序需要身份验证的时候可以自动将验证申请交给ssh-agent来完成整个认证过程。

  1. 先检查ssh-agent是否在运行:
    # start the ssh-agent in the background
    eval "$(ssh-agent -s)"
    # Agent pid 59566
    
  2. 把SSH key添加ssh-agent:
    ssh-add ~/.ssh/id_rsa
    

注意:如果你采用现有的SSH keys,你需要把id_rsa(这是一个文件名)替换成现有的私钥文件名。

如果出现

  1. Could not open a connection to your authentication agent.

    需要先执行下面的命令,才能进行ssh-add操作

    ssh-agent bash

Step 4: 把SSH key添加到github账户

这个时候需要复制id_rsa.pub的内容,即SSH key,注意这个文件不一定是id_rsa.pub,有可能是id_dsa.pub,id_ecdsa.pub or id_ed25519,需要看自己的设置。

pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

 如果pbcopy失败,可以直接用vi打开id_rsa.pub复制里面的内容。

详细步骤:

  1. Settings icon in the user bar进入个人账户的Settings.
  2. SSH keys选择SSH keys.
  3. SSH Key button点击Add SSH key.
  4. 添加title,题目可为 “Personal MacBook Air”,根据自己的情况添加。
  5. The key field复制key.
  6. The Add key button点击Add key.
  7. 最后会要求输入github的密码进行确认.

Step 5: 测试连接

在完成上述步骤后,可以测试连接,确保ssh正常工作。在尝试ssh连接时,github会提示认证。

  1. 打开终端并输入:
    ssh -T git@github.com
    # Attempts to ssh to GitHub
    
  2. 然后会看到下面的警告:
    # The authenticity of host 'github.com (207.97.227.239)' can't be established.
    # RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
    # Are you sure you want to continue connecting (yes/no)?
    

    输入yes:

    # Hi username! You've successfully authenticated, but GitHub does not
    # provide shell access.
    
  3. 看到上面的信息后证明你已经成功ssh连接到github!

If you receive a message about “access denied,” you can read these instructions for diagnosing the issue.

If you’re switching from HTTPS to SSH, you’ll now need to update your remote repository URLs. For more information, see Changing a remote’s URL.

68 Text Justification(Hard)

1.该题主要考察字符串操作。

2.主要思路是,如果单词为当前行的第一个单词,则不需要考虑插入空格,如果单词为当前行的第二个单词,则需要补充空格。这样可以尽可能多地添加单词到同一行中。

3.当完成单词的选择后,需要进行空格补充,从第二个单词开始遍历(第一个单词前面不需要补充空格),每次在单词前面补充一个空格,知道满足宽度要求。

4.在3中,条件是从第二个单词开始补充。需要注意的是,假如一个单词为一行,但是这行不是结束行,那么在这个单词后面补充足够多的空格即可。

Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactlyL characters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

For the last line of text, it should be left justified and no extra space is inserted between words.

For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16.

Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Note: Each word is guaranteed not to exceed L in length.

click to show corner cases.

Corner Cases:

  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.

 

AC代码:
[c language=”++”]
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
int newStrCount = 0;
int n = words.size();
int nowIdx = 0;
vector<string> result(0);
while (nowIdx < n)
{
int nowStrWidth = 0;
int startIdx = nowIdx;
int endIdx = nowIdx;
for (; nowIdx < n; nowIdx++)
{
if (nowStrWidth == 0 && maxWidth >= nowStrWidth + words[nowIdx].size())
{//这是这个字符串的第一个单词,包含了最后一个单词单独为一行的状态
endIdx = nowIdx;
nowStrWidth += words[nowIdx].size();

}
else if (nowStrWidth > 0 && maxWidth >= nowStrWidth + words[nowIdx].size() + 1)
{//不是字符串的第一个单词,需要补充一个空格
endIdx = nowIdx;
nowStrWidth += words[nowIdx].size() + 1;
}
else//大于的话退出循环
break;
}
string thisStr = "";//这次生成的text string
vector<string> thisStrWords(0);
for (int i = startIdx; i <= endIdx; i++)
{
thisStrWords.push_back(words[i]);
}
if (nowIdx == n)//最后一行的单词
{
thisStr += thisStrWords[0];
for (int i = 1; i < thisStrWords.size(); i++)
{//添加单词,从第二个单词开始,每个单词前面都需要补充一个空格
thisStr = thisStr+" "+thisStrWords[i];
}
for (int i = 0; i < maxWidth – nowStrWidth; i++)
thisStr += " ";//补充缺少的空格
}
else
{
for (int i = 1; i < thisStrWords.size(); i++)
{//因为之前对压进来的单词没有加空格,现在先加上
thisStrWords[i] = " " + thisStrWords[i];
}
//开始进行justification
while (nowStrWidth < maxWidth)
{
if (thisStrWords.size() > 1)
{//如果有两个单词或以上
for (int i = 1; i < thisStrWords.size(); i++)
{//在第二个单词前面补充空格,优先补充左边的空格,一旦符合要求,马上退出
if (nowStrWidth == maxWidth)//满足宽度要求
break;
thisStrWords[i] = " " + thisStrWords[i];
nowStrWidth++;
}
}
else
{//如果只有一个单词
int blankCount = maxWidth – nowStrWidth;
for (int i = 0; i < blankCount; i++)
{
nowStrWidth++;
thisStrWords[0] += " ";//补充缺少的空格
}
}
}
for (int i = 0; i < thisStrWords.size(); i++)
{//把单词合并成text string
thisStr += thisStrWords[i];
}
}
result.push_back(thisStr);
}
return result;
}
};
[/c]