合法字符串

【原创】合法字符串


// test.cpp : 定义控制台应用程序的入口点。  
// editor: Visual C++ 2010 express  
// system: win7 x32  
// create: 2013-07-29 15:20FM  
// author: jiang kejun <jinhua_k9@163.com>  
  
#include "stdafx.h"  
#include <string>  
#include <iostream>
#include <math.h>
using namespace std;  

int validstring(int n,int m);
bool in_array(string needle, string s[]);
int _tmain(int argc, _TCHAR* argv[])  
{   
    cout<<validstring(2,3)<<endl;
	cout<<validstring(3,4)<<endl;
    system("pause");  
    return 0;  
      
}  

string AZ="0abcdefghijklmnopqrstuvwxyz";
const int N=26;
int validstring(int n,int m)
{
	// 尾数长度
	int j=0;
	string ws[N];
	//string fws[N];
	// 截断长度
	int cut=0;
	// 非尾数长度
	double size=0.0;
	// 交换介质
	string tmp;
	for(int i=1;i<=n;i++){
		tmp = AZ.at(i);
		if(2*i>n){
			ws[j]=tmp;
			//fws[i]=tmp;
			j++;
			size++;
		}
		if(2*i<=n){
			
			if((i+1)>= 2*i && !in_array(tmp, ws)){
				//fws[i]=tmp;
				cut++;
				size++;
			}
		}
	}
	//int W=sizeof(ws);
	//int F=sizeof(fws);
	int C=int(pow(size, (m-1)));

	cut = (m-1<=2)?cut:(C/size-((size-cut)*(m-2)))*cut;

	return (C-cut)*j;
}

bool in_array(string needle, string s[])
{
	for(int i=0;i<26;i++)
	{
		if(s[i]!="" && s[i]==needle)
		{
			return true;
		}
	}
	return false;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值