AlgorithmTesting

// 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;
}

NIST测试是对随机生成的二进制序列进行统计测试的方法。您可以使用Matlab来进行NIST测试。具体步骤如下: 1. 确保您拥有混沌的simulink文件(即.mdl文件),然后运行它。 2. 使用师兄的Matlab程序来运行测试,注意修改程序中的相关内容,例如要运行的.mdl文件和要生成序列的文件路径。此时,您将生成一个用于测试的文件,例如testdata.txt。您也可以使用其他仿真软件生成的序列作为测试文件。 3. 打开Cygwin终端,输入cd d:/NIST_ceshibao/sts(这里是您的.assess文件所在的位置)。 4. 输入./assess 长度(例如1000000)。注意,这个长度是一组的长度,所以要确保序列的位数足够多,以完成测试。在MATLAB中生成的界面中,您会看到m1和n,这两个数相乘就是输入的总长度,但您可以根据需要进行更改。 5. 选择0,表示要输入测试的文件。 6. 输入要测试的序列文件的位置,例如d:/NIST_ceshibao/sts/data/data_out.txt。 7. 输入1,表示选择测试15项。 8. 输入0,表示设置为默认参数。 9. 输入组个数,这是将序列分组进行测试的数量,例如10,即将序列分为10组,每组长度是1000000(即上面输入的长度)。 10. 输入0,选择2进制(选择1可能会出现UNDERFLOW问题)。 11. 耐心等待结果,在D:\NIST_ceshibao\sts\experiments\AlgorithmTesting目录下查看。 12. 结果将在D:\NIST_ceshibao\sts\experiments\AlgorithmTesting目录中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值