最近写的面试算法
来源:互联网 发布:成都女司机知乎 编辑:程序博客网 时间:2024/05/21 22:59
#include <iostream>
using namespace std;
void fuc(int a, int index);
void print(int *a,int n);
int b[100];
int main()
{
int a;
cin>>a;
fuc(a,0);
return 0;
}
void fuc(int a,int index)
{
index++;//a point
if(a==1)
{
// index++;
b[index-1]=a;
// cout<<a<<" "<<index<<endl;
print(b,index);
return;
}
else if(a==0)
{
//cout<<--index<<endl;
print(b,--index);
return;
}
else
{
for(int i=1;i<=a;i++)
{
// index++;//this is different from the place a point , then why?
// cout<<i<<" ";
b[index-1]=i;
fuc(a-i,index);
}
}
}
void fuc(int n,int a[],int index=-1)
{
index++;
if(n==0||n==1)
{
a[index]=n;
for(int j=0;j<=index;j++)
cout<<a[j]<<" ";
cout<<endl;
}
else
{
for(int i=1;i<=n;i++)
{
a[index]=i;
fuc(n-i,a,index);
}
}
}
void print(int *a,int n)
{
for(int index=0;index<n;index++)
{
cout<<*(a+index)<<" ";
}
cout<<endl;
}
#include "stdio.h"
#include <iostream>
using namespace std;
void print(int res[], int num)
{
for (int i = 0; i < num; ++i)
{
printf("%d ", res[i]);
}
printf("/n");
}
// n表示总数,m表示最大因子
void split(int n, int m)
{
static int res[100]; //保存结果
static int num = -1; //当前因子下标
num++;
//递归终止条件,为0不可再分,直接输出
if(0 == n)
{
print(res, num+1);
num--;
return;
}
else
{
if(n == m)
{
// 不拆,直接输出
res[num] = m;
print(res,num+1);
num--;
}
else
{
// 拆分出第一个
res[num] = m;
n = n-m;
//最大因子不可能大于总数
if(m>n) m = n;
// 循环,第二个因子可以继续拆分,而且按照最大因子不同可以拆分成多个
for (int i = m; i>=1; --i)
{
split(n, i);
}
num--;
}
}
}
void Split(int n)
{
for (int i = n-1; i>=1; i--)
{
split(n, i);
}
}
int main()
{
int a;
cin>>a;
Split(a);
return 0;
}
void reverse(char *a,char *b)
{
if(a>=b)
{
return ;
}
else
{
char temp=*a;
char em=*b;
*a=em;
*b=temp;
reverse(++a,--b);
}
}
#include <vector>
#include <iostream>
using namespace std;
void print_all(int b[],int size);
int a[100];
void all_sort(vector<int> v,int index=0)
{
index++;
vector<int>::iterator temp;
if(v.size()==1)
{
a[index-1]=v.at(0);
print_all(a,index);
}
else
{
for(int i=0;i<v.size();i++)
{
a[index-1]=v.at(i);
vector<int> d(v);
temp=d.begin();
d.erase(temp+i);
all_sort(d,index);
}
}
}
void print_all(int b[],int size)
{
for(int i=0;i<size;i++)
cout<<b[i]<<" ";
cout<<endl;
}
void delete_sort(vector<int> v)
{
vector<int>::iterator temp=v.begin();
v.erase(temp);
vector<int>::iterator begin=v.begin(),end=v.end();
for(begin;begin!=end;begin++)
cout<<*begin<<" ";
cout<<endl;
}
int main()
{
int a;
vector<int> c;
while(cin>>a)
{
c.push_back(a);
}
all_sort(c);
/*vector<int>::iterator begin=c.begin(),end=c.end();
for(begin;begin!=end;begin++)
cout<<*begin<<" ";*/
return 0;
}
///没有重复元素
#include <vector>
#include <iostream>
using namespace std;
void print_all(int b[],int size);
int a[100];
bool isin(int* a,int *b,int value)
{
for( ;a!=b;a++)
{
if(*a==value)
return 1;
}
return 0;
}
void all_sort(vector<int> v,int index=0)
{
index++;
vector<int>::iterator temp;
if(v.size()==1)
{
a[index-1]=v.at(0);
print_all(a,index);
}
else
{
for(int i=0;i<v.size();i++)
{
vector<int>::iterator start=v.begin(),end=start+i;
if(isin(start,end,v.at(i)))
continue;
a[index-1]=v.at(i);
vector<int> d(v);
temp=d.begin();
d.erase(temp+i);
all_sort(d,index);
}
}
}
void print_all(int b[],int size)
{
for(int i=0;i<size;i++)
cout<<b[i]<<" ";
cout<<endl;
}
void delete_sort(vector<int> v)
{
vector<int>::iterator temp=v.begin();
v.erase(temp);
vector<int>::iterator begin=v.begin(),end=v.end();
for(begin;begin!=end;begin++)
cout<<*begin<<" ";
cout<<endl;
}
int main()
{
int a;
vector<int> c;
while(cin>>a)
{
c.push_back(a);
}
all_sort(c);
/*vector<int>::iterator begin=c.begin(),end=c.end();
for(begin;begin!=end;begin++)
cout<<*begin<<" ";*/
return 0;
}
// bbtree.h: interface for the bbtree class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_BBTREE_H__AB4588FD_864D_4C2D_B562_B2E8DEB974D0__INCLUDED_)
#define AFX_BBTREE_H__AB4588FD_864D_4C2D_B562_B2E8DEB974D0__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <iostream>
using namespace std;
template <class object>
class bbtree
{
public:
class avlnode
{
public:
object data;
avlnode *left;
avlnode *right;
int height;
avlnode(object &d=object(),avlnode* l=NULL,avlnode *r=NULL,int h=0):data(d), left(l),right(r),height(h)
{
}
};
avlnode *head;
void visit(avlnode *&t)
{
if(t!=NULL)
{
cout<<t->data<<endl;
visit(t->left);
visit(t->right);
}
}
public:
bbtree()
{
head=NULL;
}
virtual ~bbtree()
{
}
int height(avlnode *t) const
{
return t==NULL?-1:t->height;
}
void insert( object &x,avlnode *&t)
{
if(t==NULL)
t=new avlnode(x);
else if(x<t->data)
{
insert(x,t->left);
if(height(t->left)-height(t->right)==2)
{
if(x<t->left->data)
rotatedright(t);
else
doubleright(t);
}
}
else if(x>t->data)
{
insert(x,t->right);
if(height(t->right)-height(t->left)==2)
{
if(x>t->right->data)
{
rotatedleft(t);
}
else
{
doubleleft(t);
}
}
}
else ;
t->height=max(height(t->left),height(t->right))+1;
}
void insert(object &d)
{
insert(d,head);
}
void rotatedright(avlnode * &t)
{
avlnode *temp=t->left;
t->left=temp->right;
temp->right=t;
t->height=max(height(t->left),height(t->right))+1;
temp->height=max(height(temp->left),t->height)+1;
t=temp;
}
void rotatedleft(avlnode * &t)
{
avlnode * temp=t->right;
t->right=temp->left;
temp->left=t;
t->height=max(height(t->left),height(t->right))+1;
temp->height=max(height(temp->right),t->height)+1;
t=temp;
}
void doubleleft(avlnode *&t)
{
rotatedleft(t->right);
rotatedright(t);
}
void doubleright(avlnode *&t)
{
rotatedright(t->left);
rotatedleft(t);
}
int max(int a,int b)
{
return (a<b)?b:a;
}
void visit()
{
visit(head);
}
bool isequal(avlnode * &a,avlnode *& b)
{
if(a!=NULL&b!=NULL)
{
if(b->data!=a->data) return false;
else
{
return (isequal(a->left,b->left)&&isequal(a->right,b->right));
}
}
else if(a==NULL&&b==NULL)
{
return true;
}
else return false;
}
bool isequal(avlnode * &t)
{
return isequal(head,t);
}
};
#endif // !defined(AFX_BBTREE_H__AB4588FD_864D_4C2D_B562_B2E8DEB974D0__INCLUDED_)
template <class object>
int finddata(vector<object> &v,int size, object key)
{
int low =0;
while(low<=size)
{
if(v[(low+size)/2]==key) return (low+size)/2;
else if (v[(low+size)/2]<key) low =(low+size)/2+1;
else size=(low+size)/2-1;
}
return -1;
}
void bubble(int a[],int n)
{
int index=n,temp=-1;
for(int i=0;i<n-1;i++)
{
for(int j=1;j<index;j++)
{
if(a[j]<a[j-1])
{
swap(a[j],a[j-1]);
temp=j;
}
}
index=temp;
}
}
void setsign(vector<int> &v,vector<int> &v1)
{
bitset<MAX_VALUE+1> s;
for(int i=0;i<v.size();i++)
s.set(v[i]);
for(int j=0;j<v1.size();j++)
s.set(v1[j]);
for(int k= MAX_VALUE;k>=0;k--)
if(s[k])
cout<<k<<" ";
}
void heap(int a[],int le)
{
for(int j=le/2;j>=1;j--)
ajust(a,j,le);
for(int i=le;i>1;i--)
{
swap(a[1],a[i]);
ajust(a,1,i-1);
}
}
void ajust(int a[],int b,int c)
{
int temp=a[b], index;
for(;b<=c/2;b=index)
{
index=2*b;
if(index<=c-1&&a[index]<a[index+1])
index++;
if(temp<a[index])
a[b]=a[index];
else
break;
}
a[b]=temp;
}
void insert(int a[],int n)
{
int temp=-1,va;
for(int i=0;i<n;i++)
{
va=a[i+1];
temp=i;
while(temp>=0&&va<a[temp])
{
a[temp+1]=a[temp];
temp--;
}
a[temp+1]=va;
}
}
void insert(int a[],int n)
{
int temp=-1,va;
for(int i=0;i<n;i++)
{
va=a[i+1];
for(int j=i;va<a[j]&&j>=0;j--)
{
a[j+1]=a[j];
}
a[j+1]=va;
}
}
1 #include
2 main()
3 {
4 char x,y,z;
5 int i;
6 int a[16];
7 for(i=0;i<=16;i++)
8 {
9 a[i]=0;
10 printf("/n");
11 }
12 return 0;
13 }
让我说出运行结果,我一看,小样,这个问题我见过,如果编译器是以内存递减方
式存储的话会死循环,心里一阵窃喜,不过我还是看着屏幕,边看变说“哦,数组越界
了”,过了一会儿说,“对了,会死循环,因为a[16]就是i,在循环体中被赋0了”。接
着他继续问,那如何修改才能让它正常运行呢,我说把循环语句的=删了就可以了,然后
他接着说,7行以下的语句都不允许修改,还有没有什么办法,我说那在第6行之前再声
明一个int型的变量,虽然没用,但是a[16]就不是i了,他又说,不这么做,5语句也不
允许修改呢,我想了想几分钟实在是想不出来,只好说不知道,他就没再问了,继续下
一个问题:
// List.h: interface for the List class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_LIST_H__FECB9FBE_0198_4708_BD63_46F66EC66037__INCLUDED_)
#define AFX_LIST_H__FECB9FBE_0198_4708_BD63_46F66EC66037__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <iostream>
using namespace std;
template <typename object>
class List
{
private:
class Node
{
public:
object data;
Node *next;
Node *prev;
Node(const object & da=object(), Node *p=NULL,Node*n=NULL):data(da),prev(p),next(n)
{
}
};
int theSize;
Node * head;
Node *tail;
void init()
{
theSize=0;
head=new Node();
tail=new Node();
head->next=tail;
tail->prev=head;
}
public:
class const_iterator
{
protected:
Node *current;
object & retrive()
{
return current->data;
}
const_iterator(Node*p):current(p)
{
}
public:
const_iterator():current(NULL)
{
}
const object & operator*()
{
return retrive();
}
const_iterator operator++()
{
current=current->next;
return *this;
}
const_iterator operator++(int)
{
const_iterator old=*this;
++(*this);
return old;
}
bool operator==(const_iterator &rhs) const
{
return current==rhs.current;
}
bool operator!=(const_iterator &rhs) const
{
return current!=rhs.current;
}
friend class List<object>;
};
class iterator:public const_iterator
{
public:
iterator()
{
}
object & operator*()
{
return retrive();
}
iterator & operator++()
{
current=current->next;
return *this;
}
iterator & operator++(int)
{
iterator old=*this;
++(*this);
return old;
}
bool operator==(iterator &rhs) const
{
return current==rhs.current;
}
bool operator!=(iterator &rhs) const
{
return current!=rhs.current;
}
friend class List<object>;
protected:
iterator(Node *p):const_iterator(p)
{
}
};
iterator begin()
{
return iterator(head->next);
}
const_iterator begin() const
{
return const_iterator(head->next);
}
iterator end()
{
return iterator(tail);
}
const_iterator end() const
{
return const_iterator(tail);
}
List()
{
init();
}
List(const List &rhs)
{
init()
operator=(rhs);
}
const List & operator=(List &rhs)
{
if(this!=&rhs)
{
clear();
for(const_iterator itr=rhs.begin();itr!=rhs.end();++itr)
push_back(*itr);
return *this;
}
return rhs;
}
void push_back(const object &x)
{
insert(end(),x);
}
iterator insert(iterator itr, const object &x)
{
Node *p=itr.current;
theSize++;
return iterator(p->prev=p->prev->next=new Node(x,p->prev,p));
}
~List()
{
clear();
delete head;
delete tail;
}
void clear()
{
Node *temp=head->next,*deleteNode;
while(temp!=tail)
{
deleteNode=temp;
temp=temp->next;
delete deleteNode;
}
}
};
#endif // !defined(AFX_LIST_H__FECB9FBE_0198_4708_BD63_46F66EC66037__INCLUDED_)
void reverse()
{
Node *temp, *p=NULL;
while(head!=NULL)
{
temp=head;
head=head->next;
temp->next=p;
p=temp;
}
head=p;
}
void * memove(void * destination , void * source , size_t n)
{
char *d=(char*) destination;
char *s=(char *) source;
if(d<s)
{
while(n--)
*d++=*s++;
}
else
{
d+=n-1;
s+=n-1;
while(n--)
*d--=*s--;
}
return destination;
}
template <class T>
merger(vector<T>& v,int a,int center,int b)
{
vector<T> temp; //temp(5) difference
int x=a,y=center+1;
while(x<=center&&y<=b)
{
if(v[x]<v[y])
temp.push_back(v[x++]);
else
temp.push_back(v[y++]);
}
while(x<=center)
temp.push_back(v[x++]);
while(y<=b)
temp.push_back(v[y++]);
x=0;
while(x<temp.size())
{
v[a++]=temp[x++];
}
}
template <class T>
void mergersort(vector<T> &v,int a,int b)
{
if(a<b)
{
int center=(a+b)/2;
mergersort(v,a,center);
mergersort(v,center+1,b);
merger(v,a,center,b);
}
}
// recursion.cpp: implementation of the recursion class.
//
//////////////////////////////////////////////////////////////////////
#include <iostream>
// #include "recursion.h"
#include <vector>
#include <queue>
using namespace std;
template <class object>
void notrecursionquicksort(vector<object>&v,int a, int b);
template <class object>
void quicksort(vector<object>&v,int a, int b)
{
if(a<b)
{
int start=a;
int end=b-1;
object temp;
while(start<end)
{
while(v[start]<v[b]&&start<b) start++;
while(v[end]>v[b]&&end>=a) end--;
if(start<end) swap(v[start++],v[end--]);
else break;
}
swap(v[start],v[b]);
quicksort(v,a,start-1);
quicksort(v,start+1,b);
}
}
template <class object>
void quicksort(vector<object>&v)
{
notrecursionquicksort(v,0,v.size()-1);
}
struct node
{
int low ;
int high;
};
template <class object>
void notrecursionquicksort(vector<object>&v,int a, int b)
{
if(a<b)
{
queue<node> qu;
do
{
node temp;
if(a<b)
{
int start=a;
int end=b-1;
while(a<b)
{
while(v[start]<v[b]&&start<b) start++;
while(v[end]>v[b]&&end>=a) end--;
if(start<end) swap(v[start++],v[end--]);
else break;
}
swap(v[start],v[b]);
temp.low=start+1;
temp.high=b;
qu.push(temp);
temp.low=a;
temp.high=start-1;
qu.push(temp);
}
temp=qu.front();
qu.pop();
a=temp.low;
b=temp.high;
}while(!qu.empty());
}
}
template <class object>
bool finddata(vector<object> &v,object &t)
{
int low=0;
int high=v.size()-1;
while(low<high)
{
int mid=(low+high)/2;
if(v[mid]==t) return true;
else if (v[mid]>t) high=mid-1;
else low=mid+1;
}
return false;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
int main()
{
vector<int> v;
int a;
while(cin>>a)
{
v.push_back(a);
}
quicksort(v);
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
cout<<endl;
cin.clear();
cin>>a;
cout<<finddata(v,a)<<endl;
return 0;
}
/* #include"stdio.h"
#define Maxsize 100
void quicksort(int a[],int n)
{ int i,j,low,high,temp,front=-1,rear=-1;
struct node
{ int low,high;
}qu[Maxsize];
rear++;
qu[rear].low=0;qu[rear].high=n-1;
while(front!=rear)
{ front=(front+1)%Maxsize;
low=qu[front].low;high=qu[front].high;
i=low;j=high;
if(low<high)
{ temp=a[low];
while(i!=j)
{ while(i<j&&a[j]>temp)j--;
if(i<j){a[i]=a[j];i++;}
while(i<j&&a[i]<temp)i++;
if(i<j){a[j]=a[i];j--;}
}
a[i]=temp;
rear=(rear+1)%Maxsize;qu[rear].low=low;qu[rear].high=i-1;
rear=(rear+1)%Maxsize;qu[rear].low=i+1;qu[rear].high=high;
}
}
}
int main()
{ int a[10]={189,987,654,321,548,254,658,845,125,489};
int i,n=10;
quicksort(a,n);
for(i=0;i<10;i++)printf("%d ",a[i]);printf("/n");
}
*/
int fuc(int a,int b)
{
if(b==1) return a;
else
{
if(b%2==0) return fuc(a,b/2)*fuc(a,b/2);
else return fuc(a,b/2)*fuc(a,b/2)*a;
}
}
int fuc(int a,int b)
{
int sum=1;
int temp=b;
for(;b>0;)
b<<=1;
while(temp)
{
if(b<0)sum*=sum*a;
else sum*=sum;
b<<=1;
temp/=2;
}
return sum;
}
// quickselect.h: interface for the quickselect class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_QUICKSELECT_H__1B1215BD_F7EB_4804_AEDF_DCDF18E26A40__INCLUDED_)
#define AFX_QUICKSELECT_H__1B1215BD_F7EB_4804_AEDF_DCDF18E26A40__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <iostream>
#include <vector>
using namespace std;
template <class object>
class quickselect
{
public:
vector<object> v;
object & medien(vector<object> &v,int a,int b)
{
int temp=(a+b)/2;
if(v[a]>v[temp]) swap(v[a],v[temp]);
if(v[temp]>v[b]) swap(v[temp],v[b]);
if(v[a]>v[temp]) swap(v[a],v[temp]);
swap(v[temp],v[b-1]);
return v[b-1];
}
object & insersort(vector<object> &v,int start,int end, int index)
{
for(int i=start+1;i<=end;i++)
{
object temp=v[i];
for(int j=i;j>start&&v[j]<v[j-1];j--)
{
v[j]=v[j-1];
}
v[j]=temp;
}
return v[index+start-1];
}
const object & quickfind(vector<object> &v,int start,int end , int index)
{
if(start+3<end)
{
object & temp=medien(v,start,end);
int start1=start+1;
int end1=end-2;
while(start1<end1)
{
while (temp>v[start1])start1++;
while (temp<v[end1]) end1--;
if(start1<end1) swap(v[start1],v[end1]);
else break;
}
swap(v[start1],v[end-1]);
if(start1-start+1==index) return v[start1];
else if(start1-start+1>index)
{
return quickfind(v,start,start1-1,index);
}
else
{
return quickfind(v,start1+1,end,index-start1+start-1);
}
}
else
{
return insersort(v,start,end,index);
}
}
public:
void insert(object data)
{
v.push_back(data);
}
const object & findthenumber(int index)
{
return quickfind(v,0,v.size()-1,index);
}
quickselect()
{
}
~quickselect()
{
}
};
#endif // !defined(AFX_QUICKSELECT_H__1B1215BD_F7EB_4804_AEDF_DCDF18E26A40__INCLUDED_)
template <class T>
T media(vector<T> &v,int low,int high)
{
int center=(low+high)/2;
if(v[low]>v[center])
swap(v[low],v[center]);
if(v[high]<v[center])
swap(v[high],v[center]);
if(v[center]<v[low])
swap(v[center],v[low]);
swap(v[center],v[high-1]);
cout<<v[high-1]<<v[low]<<v[high]<<endl;
return v[high-1];
}
template <class T>
void insert(vector<T> &a,int low,int n)
{
int temp=-1,va;
for(int i=low;i<n;i++)
{
va=a[i+1];
for(int j=i;va<a[j]&&j>=low;j--)
{
a[j+1]=a[j];
}
a[j+1]=va;
}
}
template <class T>
quicksort(vector<T>& v,int low,int high)
{
if(low+2<high)
{
T pivot=media(v,low,high);
int a=low+1,b=high-2;
while(a<b)
{
for(;v[a]<pivot;)a++;
for(;v[b]>pivot;)b--;
if(a<b)swap(v[a],v[b]);
}
swap(v[a],v[high-1]);
quicksort(v,low,a-1);
quicksort(v,a+1,high);
}
else
{
insert(v,low, high);
}
}
char * removeenglishwords(char *src)
{
char *temp=new char[strlen(src)+1];
char *dest=temp;
while(*src)
{
int a=(int) *src;
if(a<0)*temp++=*src++;
else
src++;
}
*temp='/0';
return dest;
}
void revert(char * p)
{
int end=strlen(p)-1;
int start=0;
for(;start<end;start++,end--)
swap(*(p+end),*(p+start));
}
void select(int a[],int n)
{
int min;
for(int i=0;i<n-1;i++)
{ min=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[min])
min=j;
}
swap(a[i],a[min]);
}
}
template <class object>
void quicksort(vector<object>&v,int a, int b)
{
if(a<b)
{
int start=a;
int end=b-1;
while(a<b)
{
while(v[start]<v[b]&&start<b) start++;
while(v[end]>v[b]&&end>=a) end--;
if(start<end) swap(v[start++],v[end--]);
else break;
}
swap(v[start],v[b]);
quicksort(v,a,start-1);
quicksort(v,start+1,b);
}
}
// stringa.cpp: implementation of the stringa class.
//
//////////////////////////////////////////////////////////////////////
#include "stringa.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
stringa::stringa()
{
p=NULL;
}
stringa::stringa(char *r)
{
int length=strlen(r)+1;
p=new char[length];
strcpy(p,r);
}
stringa::~stringa()
{
if(p) delete [] p;
}
void stringa::operator =(const stringa &r)
{
if(p) delete []p;
char *temp1=new char[strlen(r.p)+1];
//strcpy(p,r.p);
p=temp1;
char *temp2=r.p;
while(*temp2)
{
*temp1++=*temp2++;
}
*temp1='/0';
}
ostream & operator<<(ostream &o,stringa &r)
{
char *temp=r.p;
while(*temp)
o<<*temp++;
o<<endl;
return o;
}
istream & operator>>(istream &i,stringa &r)
{
if(r.p)delete []r.p;
r.p=new char[100];
char *ptemp=r.p;
char temp;
while(cin>>temp)
{
*ptemp++=temp;
}
*ptemp='/0';
return i;
}
int main()
{
stringa a;
// a="aijisong";
char *r=new char[10];
//cin>>a;
//while(r)
int i=9;
while(i--)
cout<<(int)*r;
return 0;
}
// smatch.cpp: implementation of the smatch class.
//
//////////////////////////////////////////////////////////////////////
#include "smatch.h"
#include <stack>
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
smatch::smatch()
{
}
smatch::~smatch()
{
}
void pri(stack<char*> s)
{
while(!s.empty())
{
cout<<*s.top();
s.pop();
}
cout<<endl;
}
void stringmatch(char *a, char *b)
{
stack<char *> s;
char *aa=a;
while(*a)
{
do
{
while(*aa&&*b)
{
if(*aa!=*b)
{
aa++;
}
else
{
s.push(aa);
aa++;
b++;
}
}
if(s.empty())
break;
else
{
if(*b==0)
pri(s);
aa=++s.top();
s.pop();
b--;
}
} while(!s.empty());
a=aa;
}
}
int main()
{
char *a="aaadkk";
char *b="adk";
stringmatch(a,b);
//for(int i=0;i<=5;i++)
// cout<<*a++<<" ";
return 0;
}
// Treee.h: interface for the Treee class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_TREEE_H__50992B75_1B11_43B3_8186_C430DB749669__INCLUDED_)
#define AFX_TREEE_H__50992B75_1B11_43B3_8186_C430DB749669__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <iostream>
using namespace std;
template<class Type>
class Treee
{
private:
class tnode{
public:
Type data;
tnode *left;
tnode *right;
tnode(Type t=Type(),tnode * l=NULL,tnode *r=NULL)
{
data=t;
left=l;
right=r;
}
};
tnode *head;
void insertnode(Type t,tnode * &trp)
{
if(trp==NULL) trp=new tnode(t);
else if(t<trp->data) insertnode(t,trp->left);
else insertnode(t,trp->right);
}
tnode * & find(Type t,tnode *&trp)
{
if(trp==NULL) return trp;
else if (t==trp->data) return trp;
else if(t<trp->data) return find(t,trp->left);
else return find(t,trp->right);
}
void visit(tnode * & trp)
{
if(trp==NULL) return ;
else
{
visit(trp->left);
cout<<trp->data<<" ";
visit(trp->right);
}
}
void deletenode(Type t, tnode *&trp)
{
if(trp==NULL) return;
else if(t<trp->data) deletenode(t,trp->left);
else if(t>trp->data) deletenode(t,trp->right);
else
{
if(trp->left&&trp->right)
{
tnode * &temp=findmin(trp->right);
tnode * tempd=temp;
trp->data=temp->data;
temp=temp->right;
delete tempd;
}
else
{
tnode * temp=trp;
trp=(trp->left==NULL)?trp->right:trp->left;
delete temp;
}
}
}
tnode * & findmin(tnode * t)
{ // ASSERT(t);
while(t->left)t=t->left;
return t;
}
public:
Treee(){
head=NULL;
}
virtual ~Treee(){
}
void insertnode(Type d)
{
insertnode(d,head);
}
tnode * find(Type t)
{
return find(t,head);
}
void visit()
{
visit(head);
}
void deletenode(Type t)
{
deletenode(t,head);
}
};
#endif // !defined(AFX_TREEE_H__50992B75_1B11_43B3_8186_C430DB749669__INCLUDED_)
// Vector.h: interface for the Vector class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_VECTOR_H__1B7FE396_FDB1_45AB_9C2C_EA6CFB8BD83F__INCLUDED_)
#define AFX_VECTOR_H__1B7FE396_FDB1_45AB_9C2C_EA6CFB8BD83F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <iostream>
using namespace std;
template <typename object>
class Vector
{
enum{ SPARE=3 };
private:
int theSize;
int theCapacity;
object *objects;
public:
typedef object * iterator;
typedef const object * const_iterator;
Vector(int initSize=0):theSize(initSize),theCapacity(initSize+SPARE)
{
objects=new object[theCapacity];
}
Vector( Vector &rhs):objects(NULL)
{
operator=(rhs);
}
~Vector()
{
delete [] objects;
}
const Vector& operator=( Vector &rhs)
{
if(this==&rhs)
return rhs;
theSize=rhs.size();
theCapacity=rhs.capacity();
delete [] objects;
objects=new object[theCapacity];
for(int i=0;i<rhs.size();i++)
objects[i]=rhs.objects[i];
return *this;
}
void resize(int newSize)
{
if(newSize>theCapacity)
reserve(2*newSize+1);
theSize=newSize;
}
void reserve(int newCapacity)
{
object *oldarray=objects;
objects=new object[newCapacity];
for(int i=0;i<theSize;i++)
objects[i]=oldarray[i];
theCapacity=newCapacity;
delete [] oldarray;
}
object & operator[](int index)
{
return ojbects[index];
}
void push_back(const object &x)
{
if(theSize==theCapacity)
reserve(2*theCapacity+1);
objects[theSize++]=x;
}
void pop()
{
theSize--;
}
int capacity()
{
return theCapacity;
}
int size()
{
return theSize;
}
iterator begin()
{
return objects;
}
iterator end()
{
return &objects[theSize];
}
};
#endif // !defined(AFX_VECTOR_H__1B7FE396_FDB1_45AB_9C2C_EA6CFB8BD83F__INCLUDED_)
- 最近写的面试算法
- 最近面试的算法题目
- 最近新写的求质数算法
- 【面试】最近某大厂算法工程师面试体会
- 最近的一些面试
- 最近的面试
- 最近面试的问题
- 最近的两个面试
- 最近面试的重点
- 最近的面试总结
- 最近的笔试面试
- 谈谈最近的面试
- 最近面试的体会
- 最近的面试小结
- 最近的面试经历
- 最近写的拼图游戏
- 最近在写一款斗地主游戏,写了个癞子匹配顺子的算法
- 关于最近面试的总结
- 建立Windows下的GCC编译环境
- 哈哈,试一下
- asp.net2.0中Web.config文件标准配置简要说明
- main函数的参数
- 自动注册 ODBC 数据源 (ASA 数据库) 到 系统注册表中
- 最近写的面试算法
- Hpunix下jdbc连接数据库问题解决过程
- jdk-1_5_0-linux-i586-rpm.bin
- SQL SERVER 2005 sa登录失败!
- Wicket学习笔记(1)——WST开发问题
- zk中几种打开页面的写法
- 买不起的电子书,好视频下载
- 辅助-Code::Blocks + wxWidgets 写图形界面(GUI)程序
- 一个JavaScript读取数据库的函数