// DataStructureTesting.cpp : 定义控制台应用程序的入口点。
//
#include "stdlib.h"
#include "stdio.h"
#include <iostream>
#include <stack>
#include <string>
using namespace std;
#define OK 1
#define ERROR 0
#define MATRIX 8
/**************************************************************
链表操作 begin
***************************************************************/
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*Linklist; //在定义结构类型的同时说明结构变量
//void PrintLinklist(Linklist L)
//{
// stack<Linklist>linkNodes;
// Linklist pNode = L;
// while (pNode != NULL)
// {
// linkNodes.push(pNode);
// pNode = pNode->next;
// }
//
// while (! linkNodes.empty)
// {
// pNode = linkNodes.top();
// cout << pNode->data << endl;
// linkNodes.pop();
// }
//}
void InitLinklist(Linklist *L) //L就是头指针
{
*L = (Linklist )malloc(sizeof(LNode));//带头节点的链表
if (!(*L))
{
cout << "distributing failed!" << endl;
}
else
{
(*L)->next = NULL;
}
}
//链表头插法
void CreateLinklist_inH(Linklist *L)
{
if (*L == NULL)
{
cout << "linklist is null" << endl;
}
cout << "Please input linklist data:" << endl;
Linklist p;
int data;
while (1)
{
scanf("%d", &data);
if (data != 0)
{
p = (Linklist)malloc(sizeof(LNode));
p->data = data;
p->next = (*L)->next;
(*L)->next = p;
}
else
{
return;
}
}
}
//链表尾插法
void CreateLinklist_inR(Linklist *L)
{
if (*L == NULL)
{
return;
}
Linklist p, n;
int data;
n = *L;
while (1)
{
scanf("%d", &data);
if (data != 0)
{
p = (Linklist)malloc(sizeof(LNode));
p->data = data;
n->next = p;
n = p;
}
else
{
n->next = NULL;
return;
}
}
}
int LengthLinklist(Linklist L)
{
if (L == NULL)
{
cout << "Linklist is not initialized!" << endl;
return 0;
}
else
{
Linklist p;
p = L->next;
int length=0;
while (p != NULL)
{
length++;
printf("%d ", p->data);
p = p->next;
}
cout << endl;
return length;
}
}
//从小到大进行排序
void SortLinklist(Linklist *L)
{
Linklist p;
int temp;
int length = LengthLinklist(*L);
for (int i = 1;i < length;i++)
{
p = (*L)->next;
for (int j = 0;j < length - i;j++)
{
if (p->data > p->next->data)
{
temp = p->data;
p->data = p->next->data;
p->next->data = temp;
}
p = p->next;
}
}
p = (*L)->next;
while (p!= NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
//链表反转
void InvertLinklist(Linklist *L)
{
Linklist temp1;
Linklist p1 = *L;
Linklist p2 = p1->next;
temp1 = p2->next;
p2->next = NULL;
p1 = p2;
p2 = temp1;
while (p2)
{
temp1 = p2->next;
p2->next = p1;
p1 = p2;
p2 = temp1;
}
(*L)->next = p1;
}
//链表反转
void JOSEPHUS(Linklist *L,int n,int k,int m)
{
int data;
Linklist point1, curr,temp;
scanf("%d", &data);
point1 = (Linklist)malloc(sizeof(LNode));
point1->data = data;
(*L)->next = point1;
point1->next = point1;
curr = point1;
while (1)
{
scanf("%d", &data);
if (data != 0)
{
temp = (Linklist)malloc(sizeof(LNode));
temp->data = data;
temp->next = curr->next;
curr->next = temp;
curr = temp;
}
else
{
break;
}
}
int num = k + m;
while (--num)//从1开始
{
point1 = point1->next;
curr = point1;
}
point1 = point1->next;
curr->next = point1->next;
free(point1);
point1 = curr->next;
num = m - k;
while (--n)
{
while (--num)
{
point1 = point1->next;
curr = point1;
}
point1 = point1->next;
curr->next = point1->next;
free(point1);
point1 = curr->next;
}
}
/**************************************************************
链表操作 over
***************************************************************/
/**************************************************************
栈操作 begin
***************************************************************/
typedef struct StackNode
{
int data;
struct StackNode *next;
}StackNode,*LinkStackPtr;
typedef struct
{
LinkStackPtr top;
int count;
}LinkStack;
//void Push(LinkStack *L, int x)
//{
// LinkStackPtr p, q;
// p = (LinkStackPtr)malloc(sizeof(StackNode));
// p->data = x;
// if (L->top == NULL)
// {
// L->top = p;
// }
// else
// {
// p->next = L->top;
// L->top = p;
// L->count++;
// }
//}
void Pop(LinkStack *L,int element)
{
if (L == NULL)
{
return;
}
LinkStackPtr p = (LinkStackPtr)malloc(sizeof(StackNode));
element = L->top->data;
p = L->top;
L->top = L->top->next;
free(p);
L->count--;
}
/**************************************************************
栈操作 over
***************************************************************/
/**************************************************************
队列操作 begin
***************************************************************/
typedef struct QNode
{
int data;
struct QNode *next;
}QNode,*QuerePtr;
typedef struct
{
QuerePtr front, rear;
}LinkQuere;
/**************************************************************
队列操作 over
***************************************************************/
/**************************************************************
二叉树的操作 创建 初始化 遍历 begin
***************************************************************/
typedef struct BiTNode
{
char data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
void IniteBiTree(BiTree *T)
{
*T = NULL;
return;
}
void PreOrderTraverse(BiTree *T)
{
if (*T == NULL)
return;
printf("%c ", (*T)->data);
PreOrderTraverse(&(*T)->lchild);
PreOrderTraverse(&(*T)->rchild);
}
void CreateBiTree(BiTree *T)
{
char element;
cin >> element;
if (element == '#')
{
*T = NULL;
}
else
{
*T = (BiTree)malloc(sizeof(BiTNode));
if (!*T)
exit(0);
else
{
(*T)->data = element;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
}
/**************************************************************
二叉树的操作 创建 初始化 遍历 over
***************************************************************/
/**************************************************************
使用归并排序 begin
***************************************************************/
void Merge(int sourceArr[], int tempArr[], int startIndex, int midIndex, int endIndex)
{
int i = startIndex, j = midIndex + 1, k = startIndex;
while (i != midIndex + 1 && j != endIndex + 1)
{
if (sourceArr[i]>sourceArr[j])
tempArr[k++] = sourceArr[i++];
else
tempArr[k++] = sourceArr[j++];
}
while (i != midIndex + 1)
tempArr[k++] = sourceArr[i++];
while (j != endIndex + 1)
tempArr[k++] = sourceArr[j++];
for (i = startIndex;i <= endIndex;i++)
sourceArr[i] = tempArr[i];
}
//内部使用递归
void MergeSort(int sourceArr[], int tempArr[], int startIndex, int endIndex)
{
int midIndex;
if (startIndex<endIndex)
{
midIndex = (startIndex + endIndex) / 2;
MergeSort(sourceArr, tempArr, startIndex, midIndex);
MergeSort(sourceArr, tempArr, midIndex + 1, endIndex);
Merge(sourceArr, tempArr, startIndex, midIndex, endIndex);
}
}
/**************************************************************
归并排序 over
***************************************************************/
/**************************************************************
字符串操作 begin
***************************************************************/
//有序二维数组的查找
int MatrixFind(int **matrix, int rows, int columns, int element)
{
int row = 0;
if (matrix[row][columns - 1] > element&&columns >= 1)
{
columns--;
MatrixFind(matrix, row, columns, element);
}
else if (matrix[row][columns - 1] > element&&row < rows)
{
row++;
MatrixFind(matrix, row, columns, element);
}
return 0;
}
//将字符串中的空格转换为:%20
void TranceSpace()
{
int i = 0, countSpace = 0, indexSource = 0, indexDst = 0;
char *str = (char *)malloc(10);
cout << "Please input string:" << endl;
gets_s(str, 20);
indexSource = strlen(str);
for (i = 0; i < indexSource; i++)
{
if (str[i] == ' ')
{
countSpace++;
}
}
countSpace = 2 * countSpace;
indexDst = indexSource + countSpace;
while (indexDst > indexSource && indexSource >= 0)
{
if (str[indexSource] != ' ')
{
str[indexDst--] = str[indexSource];
}
else {
str[indexDst--] = '0';
str[indexDst--] = '2';
str[indexDst--] = '%';
}
--indexSource;
}
cout << str << endl;
}
//有两个有序的数组A1和A2,将A2中的所有数字插入到A1中并且所有的数字排序
void InsertSort()
{
char * strA = (char *)malloc(sizeof(char) * 20);
char *strB = (char *)malloc(sizeof(char) * 10);
cout << "Please input A string:" << endl;
gets_s(strA, 10);
cout << "Please input B string:" << endl;
gets_s(strB, 10);
int lengthA = strlen(strA) - 1;
int lengthB = strlen(strB) - 1;
int lengthTotal = lengthA + lengthB + 1;
strA[lengthTotal + 1] = '\0';
while (lengthA >= 0 && lengthB >= 0)
{
if (strA[lengthA] < strB[lengthB])
{
strA[lengthTotal--] = strB[lengthB];
--lengthB;
}
else
{
strA[lengthTotal--] = strA[lengthA];
--lengthA;
}
}
if (lengthA == -1)
{
for (int i = 0; i <= lengthB; i++)
{
strA[i] = strB[lengthB];
}
}
cout << strA << endl;
}
//求一个字符串中连续出现次数最多的子串
int con_sub(char *str, char **ret)
{
//str = (char *)malloc(sizeof(char ) * 10);
//cout << "Please input string:" << endl;
//gets_s(str,10);
int max_time = 0;//连续出现的最多次数
int ret_len = 0;//连续出现的字符串的长度
char *addr = NULL;//连续出现的字符串的起始地址
int len = strlen(str);
char **a = (char **)malloc(sizeof(char *)*len);
for (int i = 0;i < len;i++)//生成后缀数组
{
a[i] = &str[i];
}
for (int i = 1;i <= (len + 1) / 2;i++)
{
//当重复的字符串长度为i的时候,如果是连续出现的,那么第j和第j+i个后缀数组前面为重复的字符串
for (int j = 0;j + i <= len - 1;j += i)
{
int k = j;
int temp_time = 1;
while (k + i <= len - 1 && strncmp(a[k], a[k + i], i) == 0)
{
temp_time++;
k += i;
}
if (temp_time > max_time)
{
max_time = temp_time;
ret_len = i;
addr = a[k];
}
}
}
*ret = new char[ret_len];
strncpy(*ret, addr, ret_len);
return max_time;
}
//求一个字符串中相同且最长的字符串
char *con_lengthSub()
{
char ** substr = (char **)malloc(sizeof(char *) * 20);
char *str = (char*)malloc(sizeof(char) * 20);
gets_s(str,20);
int max_count = 0;
int ret_len = 0;
int length = strlen(str);
for (int i = 0;i <= length;i++)//利用后缀数组法
{
substr[i] = &str[i];
}
for (int i = (length + 1) / 2;i >= 0;i--)//以最长度子串为循环条件,
{
for (int j = 0;j+i <= length;j += 1)
{
int tem_count = 0;
for (int k = j + 1;k +i<= length;k++)
{
if(strncmp(substr[j], substr[k], i)==0) //若存在则直接跳出函数
{
ret_len = i;
char *arr = (char *)malloc(sizeof(char)*i);
strncpy(arr, substr[j], i);
*(arr + i) = '\0';
return arr;
}
}
}
}
}
//将一句话里的单词倒置,标点符号不倒置 begin
void InvertFunction(char *sourceWord, int n)
{
if (n == 0)
{
return;
}
int i = 0;
int j = n - 1;
char *tempWord = (char *)malloc(sizeof(char) *n);
while (j>=0)
{
tempWord[i] = sourceWord[j];
i++;
j--;
}
j = 0;
while (j < i)
{
sourceWord[j] = tempWord[j];
j++;
}
}
void InvertWord()
{
char *sourceWord = (char *)malloc(sizeof(char) * 50);
cout << "Please input string:" << endl;
gets_s(sourceWord, 50);
int wordLength = strlen(sourceWord);
InvertFunction(sourceWord, wordLength);
cout << sourceWord << endl;
int j = 0;
for (int i = 0;i < wordLength;i++)
{
if (sourceWord[i] == ' ')
{
InvertFunction(&sourceWord[j], i-j);
while (sourceWord[i] == ' ')
{
i++;
}
j = i;
}
}
cout << sourceWord << endl;
}
//转换字符串格式为原来字符串里的字符+该字符连续出现的个数 begin
void InvertChar()
{
char *resource = (char *)malloc(sizeof(char) * 50);
cout << "Please input string" << endl;
gets_s(resource, 50);
int length = strlen(resource);
char *goalStr = (char *)malloc(sizeof(char) * length * 2);
int position = 0;
int i = 0;
while(i<length)
{
int count = 1;
if (resource[i] != resource[i+1])
{
goalStr[2*position] = resource[i];
goalStr[2*position + 1] = '1';
++position;
++i;
goalStr[2 * position] = '\0';
}
else
{
while (resource[i] == resource[i+1])
{
count++;
i++;
}
goalStr[2 * position] = resource[i];
goalStr[2 * position + 1] = count + '0';
++i;
++position;
}
}
goalStr[2 * position] = '\0';
cout << goalStr << endl;
char *m = (char *)malloc(sizeof(char) * 50);
cout << "Print testing" << endl;
sprintf(m, "%d", 123);
cout << m << endl;
sprintf(m+3, "%d", 456);
cout << m << endl;
}
//最长公共子序列(LCS) 动态规划算法解LCS问题
void LCS(char * text, char * query)
{
int i, j, textLength, queryLength, start = 0, end = 0, length = 0;
textLength = strlen(text);
queryLength = strlen(query);
int matrix[MATRIX][MATRIX] = { 0 };
for (i = 0;i<textLength;i++)
{
for (j = 0;j<queryLength;j++)
{
if (text[i] == query[j])
{
if (i == 0 || j == 0)
{
matrix[i][j] = 1;
}
else
{
matrix[i][j] = matrix[i - 1][j - 1] + 1;
}
}
else
{
matrix[i][j] = 0;
}
if (matrix[i][j] > length)
{
length = matrix[i][j];
end = j;
}
}
}
start = end - length + 1;
for (i = start;i <= end;i++)
{
printf("%c", query[i]);
}
for (i = 0;i<MATRIX;i++)
{
printf("\n");
for (j = 0;j<MATRIX;j++)
{
printf("%d ", matrix[i][j]);
}
}
}
/**************************************************************
字符串操作 over
***************************************************************/
/**************************************************************
一些经典算法 begin
***************************************************************/
//走廊关灯问题
#define MaxCount 65535
void getlight(int lightCout,int people)
{
int isLight[MaxCount] = { 0 };
int i,j,count = 0;
for (i = 1;i <= people;i++)
{
for (j = i;j <= lightCout && j%i == 0;j++)
{
isLight[j-1] ^= 1;
}
}
for (i = 0;i < lightCout;i++)
{
if (isLight[i] == 1)
{
count++;
}
}
cout << count << endl;
}
char * deleteSpace(char *str)//去掉所有空格
{
if (str == NULL)
{
return NULL;
}
char *p, *temp;
p = str;
while (*p != '\0')
{
if (*p == ' ')
{
temp = p;
while (*temp == ' ')
{
do
{
*temp = *(temp + 1);
temp++;
} while (*temp != '\0');
temp = p;
}
}
p++;
}
return p;
}
void DeleteSpace1(char *str)//去掉所有空格
{
if (str == NULL)
{
return;
}
int spaceCount = 0;
char *p = str;
while (*p != '\0')
{
if (*p == ' ')
{
spaceCount++;
}
else
{
*(p - spaceCount) = *p;
}
p++;
if (*p == '\0')
{
*(p - spaceCount) = *p;
}
}
}
//删除多余的空格(开头和结尾的空格删除,中间空格只保留一个)
void DeleteSpace2(char *str)
{
bool front = true;
int count = 0;
char *p = str;
while (*p != '\0')
{
if (*p == ' ')
{
if (!front)
{
*(p - count) = *p;
count--;
}
count++;
front = true;
}
else
{
front = false;
*(p - count) = *p;
}
p++;
}
*(p - count) = *p;
p = p - count;
if (*(p - 1) == ' ')
{
*(p - 1) = *p;
p = p - 1;
}
}
//快速排序
void QuickSort(int *a, int left, int right)
{
int i = left, j = right, key,temp;
key = *(a + left);
while (i != j)
{
while (*(a + j) > key&&i < j)
{
j--;
}
while (*(a + i) < key && i < j)
{
i++;
}
if (i < j)
{
temp = *(a + i);
*(a + i) = *(a + j);
*(a + j) = temp;
}
}
*(a + left) = *(a + i);
*(a + i) = key;
QuickSort(a, left, i - 1);
QuickSort(a, i + 1, right);
}
/**************************************************************
一些经典算法 over
***************************************************************/
int ThordCount()
{
long n, t, c;
cout << "请输入以下数值:总的罪犯数目 要转移的罪行值上限 转移的罪犯数目" << endl;
scanf("%d %d %d", &n, &t, &c);
cout << "请以入狱时间输入";
cout << n;
cout << "个罪犯的罪行值" << endl;
long int m[100];
for (int i = 0;i < n;i++)
{
scanf("%d", &m[i]);
}
int thordCount = 0;
if (c >= n)
{
for (int i = 0;i < n;i++)
{
if (m[i] > t)
{
return 0;
break;
}
}
return 1;
}
bool istrue = true;
for (int i = 0;i <= n - c;i++)
{
for (int j = i;j < i+c;j++)
{
if (m[j] > t)
{
istrue = false;
break;
}
}
if (istrue)
{
thordCount++;
}
istrue = true;
}
return thordCount;
}
/
char encryptWords(char* phrase)
{
// WRITE YOUR CODE HERE
if (phrase == NULL)
{
return NULL;
}
char *phrase_temp = phrase;
char arr[] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
int i, j, k, length=0;
while (*phrase_temp != '\0')
{
length++;
phrase_temp++;
}
phrase_temp = phrase;
for (i = length-2;i >=0;i--)
{
for (j = 0;j < 26;j++)
{
if (phrase_temp[i] == arr[j])
{
int m = (j + length - 1 - i) % 26;
*(phrase_temp+i) = arr[m];
break;
}
}
}
for (i = 0;i <= length - 1;i++)
{
printf("%c", phrase[i]);
}
}
int main(int argc, char* argv[])
{
char s[]="abcdefg";
encryptWords(s);
system("pause");
return 0;
}
AlgorithmTesting
最新推荐文章于 2023-03-05 23:01:19 发布