Untitled
Darin
plain_text
2 years ago
1.8 kB
8
Indexable
package NotepadProgram;
import java.util.LinkedList;
class UserSolution {
int H, W, cursor, size;
LinkedList<Character> notepad[];
int count[][];
void init(int H, int W, char mStr[]) {
this.H = H;
this.W = W;
notepad = new LinkedList[H];
// mỗi hàng là một LinkedList
for (int i = 0; i < H; i++)
notepad[i] = new LinkedList<Character>();
count = new int[H][26];
cursor = 0;
size = 0;
int i = 0; // index của xâu cần nhập - dùng để nhập
int row = 0;
while (mStr[i] != '\0') {
if (i == (row + 1) * W)
row++;
notepad[row].add(mStr[i]); // nhập ký tự mStr[i]
count[row][mStr[i] - 'a']++;
// đếm số lần xuất hiện trong hàng của ký tự
size++;
i++;
}
}
void insert(char mChar) {
int row = cursor / W;
int col = cursor % W;
// thêm ký tự vào vị trí col của linkedlist row
notepad[row].add(col, mChar);
count[row][mChar - 'a']++;
cursor++;
size++;
// dịch phần tử tràn xuống hàng dưới
for (int i = row; i < H; i++) {
if (notepad[i].size() > W) {
char temp = notepad[i].removeLast();
count[i][temp - 'a']--;
notepad[i + 1].addFirst(temp);
count[i + 1][temp - 'a']++;
}
}
}
char moveCursor(int mRow, int mCol) {
// -1 để đưa về index 0
cursor = (mRow - 1) * W + mCol - 1;
if (cursor >= size) {
cursor = size;
return '$';
}
return notepad[mRow - 1].get(mCol - 1);
}
int countCharacter(char mChar) {
int row = cursor / W;
int col = cursor % W;
int cnt = 0;
int i = 0;
for (char c : notepad[row]) {
if (i >= col && mChar == c)
cnt++;
i++;
}
for (int j = row + 1; j < H; j++)
cnt += count[j][mChar - 'a'];
return cnt;
}
}
Editor is loading...