#include<iostream>
#include<list>
#include<string>
using namespace std;
//字符串排序
void Permutation(char *pStr,char *pBegin)
{
if(*pBegin == '\0')
cout<<pStr<<endl;
for(char *pCh = pBegin; *pCh != '\0';++pCh)
{
char temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
Permutation(pStr,pBegin+1);
temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
}
}
void Permutation(char *pStr)
{
if(pStr == NULL)
return;
Permutation(pStr,pStr);
}
//字符串组合
void Combination(char *pStr,char *pBegin,unsigned int length,list<char> &listChar)
{
if(length == 0)
{
for(list<char>::iterator iter = listChar.begin();iter != listChar.end();++iter)
cout<<*iter<<" ";
cout<<endl;
}
else
{
for(char *pCh = pBegin; *pCh != '\0';++pCh)
{
//cout<<*pCh<<" ";
listChar.push_back(*pCh);
Combination(pStr,pCh+1,length-1,listChar);
listChar.pop_back();
}
}
}
void Combination(char *pStr)
{
if(pStr == NULL)
return;
unsigned int length = strlen(pStr);
list<char> listChar;
for(int i = 1; i <= length; ++i)
{
Combination(pStr,pStr,i,listChar);
}
}
//八皇后问题
void Permutation(char *pStr,char *pBegin,int data[])
{
if(*pBegin == '\0')
{
for(int i = 0; i < 8; i++)
data[i] = pStr[i] - '0';
// int j;
bool flag = true;
for(i = 0; i < 8 && flag == true; ++i)
{
for(int j = i+1;j < 8;++j)
{
if(data[i] - data[j] == j - i || data[i] - data[j] == i - j )
{
flag = false;
break;
}
}
}
if(flag)
{
for(int k = 0; k < 8;++k)
cout<<data[k];
cout<<endl;
}
}
// cout<<pStr<<endl;
for(char *pCh = pBegin; *pCh != '\0';++pCh)
{
char temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
Permutation(pStr,pBegin+1,data);
temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
}
}
void EigthQueens()
{
int rowIndex[8];
char num[] = "01234567";
Permutation(num,num,rowIndex);
}
//字符串的快速排序
const int MAXLENGTH = 100;
int Partition(char *pStr[],int low,int high)
{
char *pKey = new char(MAXLENGTH);
strcpy(pKey,pStr[low]);
while(low<high)
{
while(low < high && strcmp(pKey,pStr[high]) <= 0)
high--;
// strcpy(pStr[low],pStr[high]);
swap(pStr[high],pStr[low]);
while(low < high && strcmp(pKey,pStr[low]) >= 0)
low++;
// strcpy(pStr[high],pStr[low]);
swap(pStr[high],pStr[low]);
}
return low;
}
void StringQucik(char *pStr[],int low,int high)
{
// int low = 0;
// int high = length
if(low < high)
{
int index = Partition(pStr,low,high);
StringQucik(pStr,low,index-1);
StringQucik(pStr,index+1,high);
}
}
void getNext(int next[],char *p)
{
int j = 0;
int k = -1;
next[0] = -1;
while(j < strlen(p)-1)
{
if(k == -1 || p[j] == p[k])
{
j++;
k++;
next[j] = k;
}
else
k = next[k];
}
}
//KMP模式匹配算法
int KMP(char *tx,char *p,int next[])
{
int i = 0;
int j = 0;
int txLen = strlen(tx);
int pLen = strlen(p);
//不能直接在while循环用strlen(),这样,如果编译器没有优化,每次都会执行一次strlen(),效率低,
//其次,strlen返回的是无符号,当j = -1时,与无符号整数比较,-1会自动转换为无符号。因此发生错误。
while(i < txLen && j < pLen)
{
if(j == -1 || tx[i] == p[j])
{
i++;
j++;
}
else
j = next[j];
}
if(j == pLen)
return i - j;
else
return -1;
}
int KMP(char *tx, char *p)
{
int pLen = strlen(p);
int *next = new int[pLen];
getNext(next,p);
return KMP(tx,p,next);
}
//最长的公共连续字符串
int GetLengthofContinusString_DP(char *pText,char *pQuery)
{
if(pText == NULL || pQuery == NULL)
return 0;
int result = 0;
unsigned int txtLen = strlen(pText);
unsigned int queryLen = strlen(pQuery);
int **dp = new int*[txtLen+1];
for(int n = 0; n <= txtLen;++n)
dp[n] = new int[queryLen+1];
for(int i = 0; i <= txtLen; ++i)
dp[i][0] = 0;
for(int j = 0; j <= queryLen; ++j)
dp[0][j] = 0;
for(i = 1; i<=txtLen; ++i)
{
for(j = 1; j <= queryLen;++j)
{
if(*(pText+i-1) == *(pQuery+j-1))
{
dp[i][j] = dp[i-1][j-1] + 1;
if( dp[i][j] > result)
result = dp[i][j];
}
else
dp[i][j] = 0;
}
}
return result;
}
void main()
{
// char str[] = "abc";
// Combination(str);
// EigthQueens();
char *p[] = {"a","ab","aa","dbb",""};
// int high = strlen(p);
StringQucik(p,0,5);
for(int i = 0; i<5;++i)
{
cout<<p[i]<<endl;
}
string str[] = {"afa","Dfeqe","D","dwwf"};
//int si = str.size();
// strlen(str);
/*
cout<<sizeof(str)/sizeof(str[0])<<endl;
int a[100]= {3,232,242,2,24,2};
cout<<sizeof(a)/sizeof(a[0])<<endl;
char *txt = "abcabcdefabcef";
char *ptxt = "abced";
cout<<KMP(txt,ptxt)<<endl;
*/
char *text = "afbckowfsfaf";
char *query = "ewljeifsf";
cout<<GetLengthofContinusString_DP(text,query)<<endl;
}