【原创】合法字符串
// 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;
}