1.删除公共字符 (pass)
删除公共字符
哈希、字符串、枚举
1.1 解析
1.2 代码
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
string s1,s2;
getline(cin,s1);
getline(cin,s2);
//将第二个字符串存放hash
// unordered_set<char> hash;
// for(auto& e:s2)
// hash.insert(e);
bool hash[300]={0};
for(char ch:s2) hash[ch]=true;
string tmp;
int n=s1.size();
for(int i=0;i<n;i++)
{
// if(!hash.count(s1[i]))
// tmp+=s1[i];
if(!hash[s1[i]]) tmp+=s1[i];
}
cout<<tmp;
return 0;
}
2.两个链表的第一个公共结点 (pass)
两个链表的第一个公共结点
链表
2.1 解析
2.2 代码
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* head1, ListNode* head2) {
//1.统计2个链表的长度
int len1=0,len2=0;
ListNode* cur=head1;
while(cur)
{
len1++;
cur=cur->next;
}
cur=head2;
while(cur)
{
len2++;
cur=cur->next;
}
//2.让长的链表先走
ListNode* maxhead=head1,*minhead=head2;
if(len2>len1)
{
maxhead=head2;
minhead=head1;
}
int tmplen=abs(len1-len2);
while(tmplen--)
{
maxhead=maxhead->next;
}
//3.让2个链表一起走
while(maxhead!=minhead)
{
maxhead=maxhead->next;
minhead=minhead->next;
}
return maxhead;
}
};
3. mari和shiny
mari和shiny
多状态线性dp
3.1 解析
3.2 代码
#include <iostream>
#include <vector>
using namespace std;
int main()
{
long long s=0,h=0,y=0;
int n=0;
cin>>n;
string str;
cin>>str;
//初始化
s=str[0]=='s'?1:0;
//2.填表
for(int i=1;i<n;i++)
{
if(str[i]=='s')s++;
else if(str[i]=='h') h+=s;
else if(str[i]=='y') y+=h;
}
cout<<y;
return 0;
}
/*int main()
{
int n=0;
cin>>n;
string str;
cin>>str;
//1.创建dp表
vector<long long> s(n);
vector<long long> h(n);
vector<long long> y(n);
//2.初始化
s[0]=str[0]=='s'?1:0;
//3.填表
for(int i=1;i<n;i++)
{
s[i]=str[i]=='s'?s[i-1]+1:s[i-1];
h[i]=str[i]=='h'?s[i-1]+h[i-1]:h[i-1];
y[i]=str[i]=='y'?h[i-1]+y[i-1]:y[i-1];
}
cout<<y[n-1];
return 0;
}*/