字符串权重

26个字母,不区分大小写,权重为1-26,每个字母的权重可以任意分配,但不能重复。任 
意输入两个单词(字母数不超过255),请设计算法,能够计算出一种权重分配方法,使这 

两个单词的权重差最大!

字符串1-字符串2

按照结果进行排序(从大到小)

赋予权重26-1

 

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

void sulotion(const string str1,const string str2);
bool mycompare(int a,int b);

int main()
{
	string str1="fjdsljflsjfljfl";
	string str2="hfdsibejhfjdshfiheu";
	sulotion(str1,str2);
	return 0;
}

void mySort(int* a,char* ch,const int nSize)
{
	for ( int i=0 ; i<nSize-1 ; ++i  ){
		for ( int j=i+1 ; j<nSize ;++j  ){
			if ( a[i]<a[j] ){
				int temp=a[i];
				a[i]=a[j];
				a[j]=temp;

				char chtemp=ch[i];
				ch[i]=ch[j];
				ch[j]=chtemp;
			}
		}
	}
}
void sulotion(const string str1,const string str2)
{
	int src[26];
	char ch[26];
	for ( int i=0 ; i<26 ;++i ){
		ch[i]='a'+i;
	}
	memset(src,0,sizeof(src));
	int strLen=str1.length();
	for ( int i=0 ; i<strLen ; ++i ){
		++src[str1[i]-'a'];
	}
	strLen=str2.length();
	for ( int i=0 ; i<strLen ; ++i ){
		--src[str2[i]-'a'];
	}
	
	mySort(src,ch,26);

	int positiveSum=0;
	int nagitiveSum=0;
	for ( int i=0 ; i<26 ; ++i ){
		if ( src[i]>0 ){
			positiveSum+=src[i];
		} else {
			nagitiveSum+=src[i];
		}
	}
	if ( -nagitiveSum<positiveSum ){
		int j=27;
		for ( int i=1 ; i<=26 ; ++i ){
			cout<<ch[i-1]<<":"<<src[i-1]<<":"<<--j<<"  ";
			if ( i%5==0 ){
				cout<<endl;
			}
		}
	} else {
		int j=0;
		for ( int i=1 ; i<=26 ; ++i ){
			cout<<ch[i-1]<<":"<<src[i-1]<<":"<<++j<<"  ";
			if ( i%5==0 ){
				cout<<endl;
			}
		}
	}
}


 

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值