1001
{A} + {B}
HDU1412
解题思路:合并两个集合然后按序输出
其实就是STL中set的应用
题解:
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int>A;
int n=0,m=0,temp;
set<int>::iterator iter;
while(cin>>n>>m)
{
for(int i=0; i<n; i++)
{
cin>>temp;
A.insert(temp);
}
for(int i=0; i<m; i++)
{
cin>>temp;
A.insert(temp);
}
iter=A.begin();
cout<<*iter;
iter++;
for(; iter!=A.end(); iter++)
{
cout<<" "<<*iter;
}
cout<<endl;
A.clear();
}
return 0;
}
1002
火车出站问题
利用栈来模拟整个过程
题解:
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main()
{
int num=0,iter=0;
string in,out;
stack<char> A;
vector<int> B;
while(cin>>num>>in>>out)
{
iter=0;
for(int i=0; i<num; i++)
{
A.push(in[i]);
B.push_back(1); // 1代表入站 -1代表出站
while(!A.empty()&&A.top()==out[iter]) //这个地方要先判断栈是否为空,然后进行取栈顶元素操作,否则会对空栈进行取栈顶操作
{
iter++;
A.pop();
B.push_back(-1);
}
}
if(A.empty())
{
cout<<"Yes."<<endl;
for(vector<int>::iterator it=B.begin(); it!=B.end(); it++)
{
if(*it>0)
{
cout<<"in"<<endl;
}
else
{
cout<<"out"<<endl;
}
}
}
else
{
cout<<"No."<<endl;
while(!A.empty())
{
A.pop();
}
}
cout<<"FINISH"<<endl;
B.clear();
}
return 0;
}
1003
统计读过多少本书
也是对set的应用
题解:
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main()
{
set<string> A;
int num;
string temp;
while(cin>>num)
{
for(int i=0; i<num; i++)
{
cin>>temp;
A.insert(temp);
}
cout<<A.size()<<endl;
A.clear();
}
return 0;
}
1004
输出 1-N的 第M个全排列
对 next_permutation的应用
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int N,M;
while(cin>>N>>M)
{
int data[N+1];
for(int i=0; i<=N; i++)
{
data[i]=i;
}
for(int i=0; i<M-1; i++)
{
next_permutation(&data[1],&data[N+1]); // 这个地方的两个参数 是全排列的开始位置和结束位置的下一个位置
}
cout<<data[1];
for(int i=2; i<=N; i++)
{
cout<<" "<<data[i];
}
cout<<endl;
}
return 0;
}
1005
火星文翻译
对map的应用
题解:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
string one,two;
map<string,string> A;
cin>>one; //读入第一个start
while(cin>>one)
{
if(one=="END")
break;
else
{
cin>>two;
A[two]=one;
}
}
cin>>one; //第二个start
string word="";
char temp;
temp=getchar(); //这个地方有一个换行符要读入 cin不会读入 导致输出结果有格式问题
while(word!="END")
{
word="";
while(scanf("%c",&temp))
{
if((temp>='a'&&temp<='z')||(temp>='A'&&temp<='Z'))
{
word+=temp;
}
else
break;
}
if(word!="END")
{
if(A.find(word)==A.end())
cout<<word;
else
cout<<A[word];
cout<<temp;
}
}
return 0;
}
1006
本质上是括号匹配问题,栈的应用,但我感觉我的处理方法可能有些蠢。
思路是:扫描字符串,如果是左括号就压栈,是右括号就去看栈顶,匹配就弹出,不匹配就将右括号变为左括号后压栈,最后操作数的结果由变换次数和栈内剩余左括号数的一半组成。
题解:
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main()
{
stack<char>A;
int Number=1;
string data;
while(cin>>data&&data[0]!='-')
{
A.push('#');
int Operation=0;
for(int i=0; i<data.length(); i++)
{
if(data[i]=='}')
{
if(A.top()=='{')
{
A.pop();
}
else
{
A.push('{');
Operation++;
}
}
else
{
A.push(data[i]);
}
}
cout<<Number<<". "<<(A.size()-1)/2+Operation<<endl;
Number++;
while(A.size()!=1)
{
A.pop();
}
}
return 0;
}
额外题:
STL的应用:
1007
思路:将数组中的数字和其对应下标构成一个映射,遍历数组的过程中在map中查找target和数组当前数的差值是否在map中能找到,并且不相等,找到即为题解。
题解:
class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
map<int,int>mp;
vector<int>A;
for(int i=0; i<nums.size(); i++)
{
mp[nums[i]]=i;
}
for(int i=0; i<nums.size(); i++)
{
if(mp.find(target-nums[i])!=mp.end()&&(mp[target-nums[i]]!=i))
{
A.push_back(i);
A.push_back(mp[target-nums[i]]);
break;
}
}
return A;
}
};
1008
石头与宝石
个人认为是map的应用;
题解:
class Solution {
public:
int numJewelsInStones(string jewels, string stones) {
map<char,int>mp;
int amount=0;
for(int i=0;i<jewels.length();i++)
{
mp[jewels[i]]=i;
}
for(int i=0;i<stones.length();i++)
{
if(mp.find(stones[i])!=mp.end())
{
amount++;
}
}
return amount;
}
};
1009
好数对
题解:
遍历两遍,统计相同的数,肯定是可以出结果的,但是复杂度是
O
(
n
²
)
O(n²)
O(n²)
在这里 用map来记录 每个数出现的次数 例如 数字A 出现了B次 那么从这B个中 任意选择两个 都可以构成一组好数对 因此 数字A可以构成
B
∗
(
B
−
1
)
/
2
B*(B-1)/2
B∗(B−1)/2组好数对。 复杂度为
O
(
n
)
O(n)
O(n)。
class Solution {
public:
int numIdenticalPairs(vector<int>& nums) {
map<int,int>mp;
for(int num:nums)
{
++mp[num];
}
map<int,int>::iterator iter;
int amount=0;
for(iter=mp.begin();iter!=mp.end();iter++)
{
amount+=(iter->second)*(iter->second-1)/2;
}
return amount;
}
};