矩阵计算

#include "stdafx.h"
#include <time.h>

#include <windows.h>
#include<iostream>
using namespace std;

void Familiar_2(int m,int ROW)
{
		int chardata[26][26]={
					{4,-2,0,-2,-1,-2,0,-2,-1,0,-1,-1,-1,-2,0,-1,-1,-1,1,0,0,0,-3,0,-2,-1},
					{-2,4,-3,4,1,-3,-1,0,-3,0,0,-4,-3,3,0,-2,0,-1,0,-1,0,-3,-4,-1,-3,1},
					{0,-3,9,-3,-4,-2,-3,-3,-1,0,-3,-1,-1,-3,0,-3,-3,-3,-1,-1,0,-1,-2,-2,-2,-3},
					{-2,4,-3,6,2,-3,-1,-1,-3,0,-1,-4,-3,1,0,-1,0,-2,0,-1,0,-3,-4,-1,-3,1},
					{-1,1,-4, 2,5,-3,-2,0,-3,0,1,-3,-2,0,0,-1,2,0,0,-1,0,-2,-3,-1,-2, 4},
					{-2,-3,-2,-3,-3,6,-3,-1,0,0,-3,0,0,-3,0,-4,-3,-3,-2,-2,0,-1,1,-1,3,-3},
					{0,-1,-3,-1,-2,-3,6,-2,-4,0,-2,-4,-3,0,0,-2,-2,-2,0,-2,0,-3,-2,-1,-3,-2},
					{-2,0,-3,-1,0,-1,-2, 8,-3,0,-1,-3,-2,1,0,-2,0,0,-1,-2,0,-3,-2,-1,2,0},
					{-1,-3,-1,-3,-3,0,-4,-3,4,0,-3,2,1,-3,0,-3,-3,-3,-2,-1,0,3,-3,-1,-1,-3},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{-1,0,-3,-1,1,-3,-2,-1,-3,0,5,-2,-1,0,0,-1,1,2,0,-1,0,-2,-3,-1,-2,1},
					{-1,-4,-1,-4,-3,0,-4,-3,2,0,-2,4,2,-3,0,-3,-2,-2,-2,-1,0,1,-2,-1,-1,-3},
					{-1,-3,-1,-3,-2,0,-3,-2,1,0,-1,2,5,-2,0,-2,0,-1,-1,-1,0,1,-1,-1,-1,-1},
					{-2,3,-3,1,0,-3,0,1,-3,0,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2;
	int n=m+1;
	int **M=new int *[n];
	for(i=0;i<n;i++)
	{
		M[i]=new int[n];
	}
	for(i=0;i<n;i++)
	{
		M[0][i]=i*d;
		M[i][0]=i*d;
	}

clock_t t1 = clock();

for(k=2;k<=2*m;k++)
{
		
	if(k<m+1)
	{	
		temp_1=1;
		temp_2=k;
	}
	
	else
	{
		temp_1=k-m;
		temp_2=m+1;
	}
		//加入并行
#pragma omp parallel for schedule(static,16)
		for(i=temp_1;i<temp_2;i++)
		{
			j=k-i;
			M[i][j]=M[i-1][j-1]+chardata[(i-1)%26][(j-1)%26];
			
			for(col=0;col<j;col++)//计算行最大
				if(M[i][col]+(j-col)*(j-col)>M[i][j])
					M[i][j]=M[i][col]+(j-col)*(j-col);

			
			
			for(row=0;row<i;row++)//计算列最大
				if(M[row][j]+(i-row)*(i-row)>M[i][j])
					M[i][j]=M[row][j]+(i-row)*(i-row);

			
			
			
		}


}
clock_t t2 = clock();
int t3=t2-t1;
cout<<t3<<endl;

	for(i=0;i<n;i++)
			cout<<M[ROW][i]<<" ";
	cout<<endl;



	
		for(i=0;i<n;i++)
		{
			delete [] M[i];
		}
		delete [] M;

}




void Familiar_1(int m,int ROW)
{
	
	
	
	int chardata[26][26]={
					{4,-2,0,-2,-1,-2,0,-2,-1,0,-1,-1,-1,-2,0,-1,-1,-1,1,0,0,0,-3,0,-2,-1},
					{-2,4,-3,4,1,-3,-1,0,-3,0,0,-4,-3,3,0,-2,0,-1,0,-1,0,-3,-4,-1,-3,1},
					{0,-3,9,-3,-4,-2,-3,-3,-1,0,-3,-1,-1,-3,0,-3,-3,-3,-1,-1,0,-1,-2,-2,-2,-3},
					{-2,4,-3,6,2,-3,-1,-1,-3,0,-1,-4,-3,1,0,-1,0,-2,0,-1,0,-3,-4,-1,-3,1},
					{-1,1,-4, 2,5,-3,-2,0,-3,0,1,-3,-2,0,0,-1,2,0,0,-1,0,-2,-3,-1,-2, 4},
					{-2,-3,-2,-3,-3,6,-3,-1,0,0,-3,0,0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-1,-1, 1, 5, 0, 0,-2, 0,-2,-1},
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
					{ 0,-3,-1,-3,-2,-1,-3,-3, 3, 0,-2, 1, 1,-3, 0,-2,-2,-3,-2, 0, 0, 4,-3,-1,-1,-2},
					{ -3,-4,-2,-4,-3, 1,-2,-2,-3, 0,-3,-2,-1,-4, 0,-4,-2,-3,-3,-2, 0,-3,11,-2, 2,-3},
					{ 0,-1,-2,-1,-1,-1,-1,-1,-1, 0,-1,-1,-1,-1, 0,-2,-1,-1, 0, 0, 0,-1,-2,-1,-1,-1},
					{ -2,-3,-2,-3,-2, 3,-3, 2,-1, 0,-2,-1,-1,-2, 0,-3,-1,-2,-2,-2, 0,-1, 2,-1, 7,-2},
					{ -1, 1,-3, 1, 4,-3,-2, 0,-3, 0, 1,-3,-1, 0, 0,-1, 3, 0, 0,-1, 0,-2,-3,-1,-2, 4}};

	int d=-8;
	int i,j,k,row,col,temp_1,temp_2;
	int n=m+1;
	int **M=new int *[n];
	for(i=0;i<n;i++)
	{
		M[i]=new int[n];
	}
	for(i=0;i<n;i++)
	{
		M[0][i]=i*d;
		M[i][0]=i*d;
		
	}

	



clock_t t1 = clock();

for(k=2;k<=2*m;k++)
{
		
	if(k<m+1)
	{	
		temp_1=1;
		temp_2=k;
	}
	
	else
	{
		temp_1=k-m;
		temp_2=m+1;
	}
		//加入并行
#pragma omp parallel for 
		for(i=temp_1;i<temp_2;i++)
		{
			j=k-i;
			M[i][j]=M[i-1][j-1]+chardata[(i-1)%26][(j-1)%26];
			
			for(col=0;col<j;col++)//计算行最大
				if(M[i][col]+d>M[i][j])
					M[i][j]=M[i][col]+d;

			
			
			for(row=0;row<i;row++)//计算列最大
				if(M[row][j]+d>M[i][j])
				M[i][j]=M[row][j]+d;

		
		}


}
clock_t t2 = clock();
int t3=t2-t1;
cout<<t3<<endl;

			
	for(i=0;i<n;i++)
			cout<<M[ROW][i]<<" ";
	cout<<endl;



		for(i=0;i<n;i++)
		{
			delete [] M[i];
		}
		delete [] M;
	
		
}




int main(){
	int demension,row;
	cout<<"这个程序输出两个序列的相似度,并且算出相似度矩阵,请输入维数:"<<endl;
	cin>>demension;
	cout<<"请输入要打印出来的行数:"<<endl;
	cin>>row;
	Familiar_1(demension,row);
	//Familiar_2(demension,row);
	return 0;
}

转载于:https://my.oschina.net/yanjianhai/blog/93315

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值