http://pat.zju.edu.cn/contests/pat-practise/1032
求两个链表的第一公共节点
#include<iostream>
#include<cstring>
using namespace std;
struct node{
char data;
int next;
};
node linkednode[100001];
int main()
{
int head1,head2,n;
int address,next;
char data;
cin>>head1>>head2>>n;
while(n--)
{
cin>>address>>data>>next;
linkednode[address].data=data;
linkednode[address].next=next;
}
if(head1==-1||head2==-1)
{
cout<<"-1"<<endl;
return 0;
}
int len1=0;
int len2=0;
int temp=head1;
while(linkednode[temp].next!=-1)
{
temp=linkednode[temp].next;
len1++;
}
temp=head2;
while(linkednode[temp].next!=-1)
{
temp=linkednode[temp].next;
len2++;
}
int diff=0;
if(len1>=len2)
{
diff=len1-len2;
while(diff--)
{
head1=linkednode[head1].next;
}
}else{
diff=len2-len1;
while(diff--)
{
head2=linkednode[head2].next;
}
}
while(head1!=-1&&head2!=-1)
{
if(head1==head2)
{
cout<<head1<<endl;
return 0;
}
head1=linkednode[head1].next;
head2=linkednode[head2].next;
}
cout<<"-1"<<endl;
return 0;
}
http://pat.zju.edu.cn/contests/pat-practise/1039
实现代码: 判断一个序列是否是某个入栈序列的合法出栈序列。
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
template<typename T,int size1,int size2>
bool outisin(T (&in)[size1], T (&out)[size2]) { // 非常棒,这种方法传递数组长度
stack<T> sin;
bool flag = false;
int len1=size1;
int len2=size2;
if (len1 == 0 || len2 == 0)
flag = true;
int innum = 0;
int num = 0;
for (int i = 0; i < len2; i++) {
if (sin.empty()&& innum < len1)
sin.push(in[innum++]); //加入第一个元素
while (sin.top() !=out[i] && innum < len1) {
sin.push(in[innum++]);
}
if (sin.top() ==out[i]) {
num++;
sin.pop();
}
}
if (num == len1)
flag = true;
return flag;
}
int main() {
int in[5] = { 1, 2, 3, 4, 5 };
int out[5] = { 4, 5, 3, 1,2 };
cout << outisin(in, out)<< endl;
return 0;
}
两个数组,输出合法的栈的操作序列 amazon 笔试题目
private String calculateOperationSequence(int[]originalArray,
int[] resultArray){
// your codeis here
Stack<Integer>stack=new Stack<Integer>();
intlen1=originalArray.length;
intlen2=resultArray.length;
int originalNum=0;
int totalNum=0;
String outString="";
for(inti=0;i<len2;i++)
{
if(stack.empty()&&originalNum<len1)
{
intinnum=originalArray[originalNum++];
stack.push(innum);
outString=outString+"push"+innum+"|";
}
while(stack.peek()!=resultArray[i]&&originalNum<len1)
{
intinnum=originalArray[originalNum++];
stack.push(innum);
outString=outString+"push"+innum+"|";
}
if(stack.peek()==resultArray[i])
{
totalNum++;
stack.pop();
outString=outString+"pop"+resultArray[i]+"|";
}
}
if(totalNum<len1)
return "None";
outString=outString.substring(0,outString.length()-1);
return outString;
}
1007. Maximum Subsequence Sum (25)
#include<iostream>
using namespace std;
int main()
{
int n;
int tempsum=0;
int maxsum=-2147483648;
int first=0;
int firsttemp=0;
int last=0;
cin>>n;
int *a=(int *)malloc(sizeof(int)*n); //
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
tempsum+=a[i];
if(tempsum<0)
{
tempsum=0;
firsttemp=i+1;// i++ was wrong, 这里需要非常注意
}
else if(tempsum>maxsum)
{
maxsum=tempsum;
first=firsttemp;
last=i;
}
}
if(maxsum<0)
{
cout<<0<<""<<a[0]<<" "<<a[n-1]<<endl; //题目的意思有要求
}else
{
cout<<maxsum<<""<<a[first]<<" "<<a[last]<<endl;
// 题目要求输出 first last 位置处的值,而不是first 和last
}
return 0;
}
判断一个图中是否有两个环,并且两个环中彼此没有公共点。
图的深度优先遍历:
#include<iostream>
#include<cstring>
using namespace std;
int map[1000][1000];
int v[1000];
int n;
int num;
void dfs(int i){
if(!v[i])
v[i]++;// v[i]=1;
for(int j=0;j<n;j++)
{
if(map[i][j]==1&&!v[j]){ //如果没访问,继续访问
dfs(j);
}
/**
* 这里有问题,不能这样做,因为所有访问的节点都会加一,并不是最先访问的节点才加一
* 如果能做到对于一个图,将最先遍历的节点记录下来,如果此第一节点访问两次,那么才可以判断
* 现在没有做这样的判断,有问题
*
if(map[i][j]==1&&v[j]) //已经访问过的节点,访问次数加1,但不递归深度
{
v[j]++;
}
*/
}
}
void dfsGra(){
num=0;
for(int i=0;i<n;i++)
if(!v[i]){
dfs(i);
num++;//count 记录联通分量个数
}
}
int main()
{
int k,t,f;
cin>>n>>k;
memset(map,0,sizeof(map));
memset(v,0,sizeof(v));
while(k--)
{
cin>>f>>t;
map[f-1][t-1]=map[t-1][f-1]=1;
}
dfsGra();
cout<<"有连通分量"<<num<<"个"<<endl;
return 0;
}