418 Sentence Screen Fitting

// 利用一个数组记录从0。。。col – 1位置开始排列setences时,最后一个string的位置

// 对于“a” 2000 2000 这样的数据依然不能通过

class Solution { 
public:
	int getNumber(vector<string> setences, int row, int col) {
		vector<pair<int, int>> record(col);
		int whichRow, 
			  whichCol;
		for (int start = 0;  start < col;  ++start) {
			whichRow = 0;
			whichCol = start;
			for (int index = 0; index < setences.size() && whichRow < row; ) {
				whichCol += setences[index].size();
				if (whichCol <= col)
					index++;
				++whichCol;
				if (whichCol >= col) {
					whichCol = 0;
					whichRow++;
				}
			}
			record[start] = make_pair(whichRow, whichCol);

		}
		int cnt = 0;
		whichRow = 0;
		whichCol = 0;
		while (whichRow < row) {
			whichRow += record[whichCol].first;
			if (whichRow < row || (whichRow == row && record[whichCol].second == 0)) 
				cnt++;
			whichCol = record[whichCol].second;
		}
		return cnt;
	}
};
点赞