Notepad Program
#include <iostream>
#define MAXL 302
using namespace std;
struct CircularQueue{
char characters[MAXL];
int count[26];
int front, rear;
void init(){
front = rear = 0;
for(int i = 0; i < 26; ++i){
count[i] = 0;
}
}
int next(int x){
return x != MAXL - 1? x + 1: 0;
}
int prev(int x){
return x != 0? x - 1: MAXL - 1;
}
char back(){
return characters[prev(front)];
}
char& operator[](int p){
int idx = rear + p;
if(idx >= MAXL){
idx -= MAXL;
}
return characters[idx];
}
int size(){
int s = front - rear;
if(s < 0){
s += MAXL;
}
return s;
}
void push_front(char c){
++count[c-'a'];
characters[rear] = c;
rear = prev(rear);
}
void push_back(char c){
++count[c - 'a'];
characters[front] = c;
front = next(front);
}
void pop_back(){
front = prev(front);
--count[characters[front] - 'a'];
}
void insert(int pos, int ch){
++count[ch - 'a'];
int idx = rear + pos;
if(idx >= MAXL){
idx -= MAXL;
}
for(int i = front; i != idx;){
int j = prev(i);
characters[i] = characters[j];
i = j;
}
characters[idx] = ch;
front = next(front);
}
}Lines[MAXL];
int length, H, W, cursor;
void init(int H, int W, char mStr[])
{
for(int i = 0; i < MAXL; ++i){
Lines[i].init();
}
length = cursor = 0;
while (mStr[length])
{
Lines[length/W].push_back(mStr[length]);
++length;
}
::H = H, ::W = W;
}
void insert(char mChar)
{
int r = cursor / W, c = cursor % W;
Lines[r].insert(c, mChar);
int p = r;
while (Lines[p].size() > W)
{
char ch = Lines[p].back();
Lines[p+1].push_front(ch);
Lines[p++].pop_back();
}
++cursor, ++length;
}
char moveCursor(int mRow, int mCol)
{
cursor = (mRow - 1) * W + mCol -1;
if(cursor >= length){
cursor = length;
return '$';
}
return Lines[cursor/W][cursor % W];
}
int countCharacter(char mChar)
{
int r = cursor / W, c = cursor % W;
int cnt = 0;
for(int i = c; i < Lines[r].size(); ++i){
if(Lines[r][i] == mChar)++cnt;
}
for(int i = r + 1; i < H; ++i){
cnt += Lines[i].count[mChar - 'a'];
}
return cnt;
}
int main(){
init(3, 4, "bcdeg");
cout << countCharacter('b') << endl;
cout << moveCursor(2, 1) << endl;
insert('f');
moveCursor(1, 4);
countCharacter('f');
moveCursor(2, 4);
insert('e');
insert('b');
countCharacter('b');
moveCursor(3, 1);
moveCursor(1, 1);
insert('a');
insert('b');
insert('a');
countCharacter('b');
moveCursor(1, 1);
countCharacter('b');
countCharacter('a');
return 0;
}
//////
#include <iostream>
#define MAX_SOLDIERS 100001
#define MAX_SCORE 6
#define MAX_TEAM 6
using namespace std;
struct Soldier{
int mTeam;
int next, prev;
}Soldiers[MAX_SOLDIERS];
int head[MAX_TEAM][MAX_SCORE], tail[MAX_TEAM][MAX_SCORE];
void init()
{
for(int i = 1; i < MAX_TEAM; ++i){
for(int j = 1; j < MAX_SCORE; ++j){
head[i][j] = 0;
tail[i][j] = 0;
}
}
}
void hire(int mID, int mTeam, int mScore)
{
Soldiers[mID].mTeam = mTeam;
Soldiers[mID].prev = Soldiers[mID].next = 0;
if(head[mTeam][mScore] == 0){
head[mTeam][mScore] = tail[mTeam][mScore] = mID;
return;
}
Soldiers[tail[mTeam][mScore]].next = mID;
Soldiers[mID].prev = tail[mTeam][mScore];
tail[mTeam][mScore] = mID;
}
void fire(int mID)
{
int mTeam = Soldiers[mID].mTeam;
for(int score = 1; score < MAX_SCORE; ++score){
if(head[mTeam][score] == 0){
continue;
}
if(head[mTeam][score] == tail[mTeam][score] && head[mTeam][score] == mID){
head[mTeam][score] = tail[mTeam][score] = 0;
return;
}
if(head[mTeam][score] == mID){
head[mTeam][score] = Soldiers[mID].next;
Soldiers[Soldiers[mID].next].prev = 0;
return;
}
if(tail[mTeam][score] == mID){
tail[mTeam][score] = Soldiers[mID].prev;
Soldiers[Soldiers[mID].prev].next = 0;
return;
}
}
Soldiers[Soldiers[mID].prev].next = Soldiers[mID].next;
Soldiers[Soldiers[mID].next].prev = Soldiers[mID].prev;
}
void updateSoldier(int mID, int mScore)
{
int mTeam = Soldiers[mID].mTeam;
fire(mID);
hire(mID, mTeam, mScore);
}
void connect(const int& mTeam,const int& src,const int& dst){
if(head[mTeam][src]==0 || src == dst){
return;
}
if(head[mTeam][dst] == 0){
head[mTeam][dst] = dst;
}
}
void updateTeam(int mTeam, int mChangeScore)
{
if(mChangeScore == 0){
return;
}
if(mChangeScore < 0){
for(int score = 1; score < MAX_SCORE; ++score){
int lastScore = score + mChangeScore;
if(lastScore < 1){
lastScore = 1;
}
connect(mTeam, score, lastScore);
}
}
}
int bestSoldier(int mTeam)
{
return 0;
}
int main(){
init();
hire(16, 1, 5);
hire(21, 1, 5);
//hire(20, 1, 5);
//cout << bestSoldier(1) << endl;
fire(21);
cout << bestSoldier(1) << endl;
hire(25, 1, 4);
hire(30, 1, 2);
updateTeam(1, 1);
cout << bestSoldier(1) << endl;
updateTeam(1, 2);
cout << bestSoldier(1) << endl;
updateSoldier(30, 2);
cout << bestSoldier(1) << endl;
updateTeam(1, -4);
hire(1, 1, 3);
updateTeam(1, -1);
cout << bestSoldier(1) << endl;
hire(100000, 5, 1);
cout << bestSoldier(5) << endl;
return 0;
}
//
//#define MAX_SOL 100003
//#define MAX_TEAM 6
//#define MAX_SCORE 6
//struct SOLDIER
//{
// int team;
// int next, pre;
//} Soldier[MAX_SOL];
//
//int HeadTeam[MAX_TEAM][MAX_SCORE];
//int TailTeam[MAX_TEAM][MAX_SCORE];
//void init()
//{
// for (int i = 1; i < MAX_TEAM; i++ )
// {
// for (int j = 1; j < MAX_SCORE; j++)
// {
// HeadTeam[i][j] = 0;
// TailTeam[i][j] = 0;
// }
// }
//}
//
//void hire(int mID, int mTeam, int mScore)
//{
// Soldier[mID].team = mTeam;
// Soldier[mID].next = 0, Soldier[mID].pre = 0;
//
// if (TailTeam[mTeam][mScore] == 0) // lisst rong-> them vao dau
// {
// HeadTeam[mTeam][mScore] = mID;
// TailTeam[mTeam][mScore] = mID;
// }
// else // them Soldier moi vao duoi cua list co diem la mScore
// {
// int lastID = TailTeam[mTeam][mScore];
// Soldier[lastID].next = mID;
// Soldier[mID].pre = lastID;
// TailTeam[mTeam][mScore] = mID;
// }
//}
//
//void fire(int mID)
//{
// int mTeam = Soldier[mID].team;
// int pre = Soldier[mID].pre;
// int next = Soldier[mID].next;
// bool isMid = true;
// for (int score = 1; score < MAX_SCORE; score++)
// {
// if (HeadTeam[mTeam][score] == mID)
// {
// isMid = false;
// HeadTeam[mTeam][score] = next;;
// Soldier[next].pre = 0;
// }
// if (TailTeam[mTeam][score] == mID)
// {
// isMid = false;
// TailTeam[mTeam][score] = pre;
// Soldier[pre].next = 0;
// }
//
// }
// if (isMid == true)
// {
// Soldier[pre].next = next;
// Soldier[next].pre = pre;
// }
//
//}
//
//void updateSoldier(int mID, int mScore)
//{
// int mTeam = Soldier[mID].team;
// fire(mID);
// hire (mID,mTeam, mScore);
//}
//
//void move (int mTeam, int src, int dst)
//{
// if (TailTeam[mTeam][src] == 0) return;
// if (TailTeam[mTeam][dst] == 0)
// {
// HeadTeam[mTeam][dst] = HeadTeam[mTeam][src];
// TailTeam[mTeam][dst] = TailTeam[mTeam][src];
//
// }
// else
// {
// int idTailDst = TailTeam[mTeam][dst];
// int idHeadSrc = HeadTeam[mTeam][src];
// Soldier[idTailDst].next = idHeadSrc;
// Soldier[idHeadSrc].pre = idTailDst;
// TailTeam[mTeam][dst] = TailTeam[mTeam][src];
// }
// HeadTeam[mTeam][src] = TailTeam[mTeam][src] = 0;
//}
//void updateTeam(int mTeam, int mChangeScore)
//{
// if (mChangeScore > 0 )
// {
// for (int i = 5; i >= 1; i--)
// {
// int newScore = i + mChangeScore ;
// if (newScore > 5)
// {
// newScore = 5;
// }
// if (i != newScore)
// {
// move(mTeam, i, newScore);
// }
// }
// }
// else
// {
// for (int i = 1; i <= 5; i++)
// {
// int newScore = i + mChangeScore ;
// if (newScore < 1)
// {
// newScore = 1;
// }
// if (i != newScore)
// {
// move(mTeam, i, newScore);
// }
// }
// }
//}
//
//int bestSoldier(int mTeam)
//{
// int bestID = 0;
// for (int i = 5; i >= 1; i--)
// {
// bestID = HeadTeam[mTeam][i];
// for (int id = HeadTeam[mTeam][i]; id; id = Soldier[id].next)
// {
// if (id > bestID)
//
// {
// bestID = id;
// }
// }
// if (bestID > 0) break;
// }
// return bestID;
//}
//#include <iostream>
//#define MAX_SOLDIER 100001
//#define MAX_SCORE 6
//#define MAX_TEAM 6
//using namespace std;
//struct SOLDIER{
// int mID;
// int mTeam;
// SOLDIER* prev;
// SOLDIER* next;
// void set(int mID, int mTeam){
// this->mID = mID;
// this->mTeam = mTeam;
// prev = next = nullptr;
// }
//}*TABLESOLDIER[MAX_TEAM][MAX_SCORE], *TAIL[MAX_TEAM][MAX_SCORE], SOLDIERS[MAX_SOLDIER];
//
//void init()
//{
// for(int i = 1; i <= MAX_TEAM; ++i){
// for(int j = 1; j <= MAX_SCORE; ++j){
// TABLESOLDIER[i][j] = nullptr;
// TAIL[i][j] = nullptr;
// }
// }
//}
//
//void hire(int mID, int mTeam, int mScore)
//{
// SOLDIERS[mID].set(mID, mTeam);
// if(TAIL[mTeam][mScore] == nullptr){
// TABLESOLDIER[mTeam][mScore] = &SOLDIERS[mID];
// TAIL[mTeam][mScore] = &SOLDIERS[mID];
// return;
// }
// TAIL[mTeam][mScore]->next = &SOLDIERS[mID];
// SOLDIERS[mID].prev = TAIL[mTeam][mScore];
// TAIL[mTeam][mScore] = &SOLDIERS[mID];
//}
//void fire(int mID)
//{
// int mTeam = SOLDIERS[mID].mTeam;
// for(int i = 1; i < MAX_SCORE; ++i){
// if(TABLESOLDIER[mTeam][i] == nullptr){
// continue;
// }
// if(TABLESOLDIER[mTeam][i] == TAIL[mTeam][i] && TABLESOLDIER[mTeam][i]->mID == mID){
// TABLESOLDIER[mTeam][i] = TAIL[mTeam][i] = nullptr;
// return;
// }
// if(TABLESOLDIER[mTeam][i]->mID == mID){
// TABLESOLDIER[mTeam][i] = TABLESOLDIER[mTeam][i]->next;
// TABLESOLDIER[mTeam][i]->prev = nullptr;
// return;
// }
// else if(TAIL[mTeam][i]->mID == mID){
// TAIL[mTeam][i] = TAIL[mTeam][i]->prev;
// TAIL[mTeam][i]->next = nullptr;
// return;
// }
// }
// SOLDIERS[mID].prev->next = SOLDIERS[mID].next;
// SOLDIERS[mID].next->prev = SOLDIERS[mID].prev;
//}
//
//void updateSoldier(int mID, int mScore)
//{
// int team = SOLDIERS[mID].mTeam;
// fire(mID);
// hire(mID, team, mScore);
//}
//
//void connect(int mTeam, int src, int dst){
// if(src == dst || TABLESOLDIER[mTeam][src] == nullptr){
// return;
// }
// if(TABLESOLDIER[mTeam][dst] == nullptr){
// TABLESOLDIER[mTeam][dst] = TABLESOLDIER[mTeam][src];
// TAIL[mTeam][dst] = TAIL[mTeam][src];
// TABLESOLDIER[mTeam][src] = TAIL[mTeam][src] = nullptr;
// return;
// }
// TABLESOLDIER[mTeam][src]->prev = TAIL[mTeam][dst];
// TAIL[mTeam][dst]->next = TABLESOLDIER[mTeam][src];
// TAIL[mTeam][dst] = TAIL[mTeam][src];
// TABLESOLDIER[mTeam][src] = TAIL[mTeam][src] = nullptr;
//}
//
//void updateTeam(int mTeam, int mChangeScore)
//{
// if(mChangeScore == 0){
// return;
// }
// if(mChangeScore <= -1){
// for(int i = 1; i < MAX_SCORE; ++i){
// int lastScore = mChangeScore + i;
// if(lastScore < 1){
// lastScore = 1;
// }
// connect(mTeam, i, lastScore);
// }
// }
// else if(mChangeScore >= 1){
// for(int i = MAX_SCORE - 1; i >= 1; --i){
// int lastScore = mChangeScore + i;
// if(lastScore > 5){
// lastScore = 5;
// }
// connect(mTeam, i, lastScore);
// }
// }
//}
//
//int bestSoldier(int mTeam)
//{
// struct SOLDIER* tm;
// if(TABLESOLDIER[mTeam][5] != nullptr){
// tm = TABLESOLDIER[mTeam][5];
// }
// else if(TABLESOLDIER[mTeam][4] != nullptr){
// tm = TABLESOLDIER[mTeam][4];
// }
// else if(TABLESOLDIER[mTeam][3] != nullptr){
// tm = TABLESOLDIER[mTeam][3];
// }
// else if(TABLESOLDIER[mTeam][2] != nullptr){
// tm = TABLESOLDIER[mTeam][2];
// }
// else if(TABLESOLDIER[mTeam][1] != nullptr){
// tm = TABLESOLDIER[mTeam][1];
// }
// int maxID = 0;
// while (tm != nullptr)
// {
// if(maxID < tm->mID){
// maxID = tm->mID;
// }
// tm = tm->next;
// }
// return maxID;
//}
//int main(){
// init();
// hire(16, 1, 5);
// hire(21, 1, 5);
// cout << bestSoldier(1) << endl;
// fire(21);
// cout << bestSoldier(1) << endl;
// hire(25, 1, 4);
// hire(30, 1, 2);
// updateTeam(1, 1);
// cout << bestSoldier(1) << endl;
// updateTeam(1, 2);
// cout << bestSoldier(1) << endl;
// updateSoldier(30, 2);
// cout << bestSoldier(1) << endl;
// updateTeam(1, -4);
// hire(1, 1, 3);
// updateTeam(1, -1);
// cout << bestSoldier(1) << endl;
// hire(100000, 5, 1);
// cout << bestSoldier(5) << endl;
//
// return 0;
//}