1. Broken Keyboard
On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.
Now given a string that you are supposed to type, and the string that you actually type out, please list those keys which are for sure worn out.
Input Specification:
Each input file contains one test case. For each case, the 1st line contains the original string, and the 2nd line contains the typed-out string. Each string contains no more than 80 characters which are either English letters [A-Z] (case insensitive), digital numbers [0-9], or _ (representing the space). It is guaranteed that both strings are non-empty.
Output Specification:
For each test case, print in one line the keys that are worn out, in the order of being detected. The English letters must be capitalized. Each worn out key must be printed once only. It is guaranteed that there is at least one worn out key.
Sample Input:
7_This_is_a_test
_hs_s_a_es
Sample Output:
7TI
签到题,不写注释
#include<bits/stdc++.h>
using namespace std;
string in,out;
unordered_map<char,int> vis;
int main(){
cin>>in>>out;
unordered_set<char> st;
for(int i=0;i<in.length();i++)
if(in[i]-'a'>=0&&'z'-in[i]>=0)
in[i]-=32;
for(int i=0;i<out.length();i++)
if(out[i]-'a'>=0&&'z'-out[i]>=0)
out[i]-=32;
int now=0;
for(int i=0;i<out.length();i++)
vis[out[i]] = 1;
for(int i=0;i<in.length();i++){
if(!vis[in[i]]){
cout<<in[i];
vis[in[i]]=1;
}
}
}
2. Perfect Sequence
Given a sequence of positive integers and another positive integer p. The sequence is said to be a perfect sequence if M≤m×p where M and m are the maximum and minimum numbers in the sequence, respectively.
Now given a sequence and a parameter p, you are supposed to find from the sequence as many numbers as possible to form a perfect subsequence.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers N and p, where N(≤10^5) is the number of integers in the sequence, and p(≤10^9) is the parameter. In the second line there are N positive integers, each is no greater than 10^9
.
Output Specification:
For each test case, print in one line the maximum number of integers that can be chosen to form a perfect subsequence.
Sample Input:
10 8
2 3 20 4 5 1 6 7 8 9
Sample Output:
8
因为只关心最大最小,所以可以先排序,然后遍历判断,注意剪枝,不满足马上跳出。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,p,cnt=0,tool,arr[100001];
int main(){
cin>>n>>p;
for(int i=0;i<n;i++)
cin>>arr[i];
sort(arr,arr+n);
for(int i=0;i<n;i++)
for(int j=i+cnt;j<n;j++)
if(arr[j]<=arr[i]*p) cnt++;
else break;
cout<<cnt;
}
3. Tree Traversals Again
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.
Figure 1
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: “Push X” where X is the index of the node being pushed onto the stack; or “Pop” meaning to pop one node from the stack.
Output Specification:
For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop
Sample Output:
3 4 2 6 5 1
入栈顺序是先序遍历,出栈是中序遍历,这样就简单了
#include<bits/stdc++.h>
using namespace std;
int pre[31];
int pop_seq[31];
int left_c[31];
int right_c[31];
string type;
stack<int> stk;
vector<int> re;
int pos[31];
int N,num,pop_num=1,pre_num=1,jud=0;
void dfs(int L,int R,int inL,int inR){
if(L==R) return;
if(pos[pre[L]]==inL){
right_c[pre[L]] = pre[L+1];
dfs(L+1,R,inL+1,inR);
}else if(pos[pre[L]]==inR){
left_c[pre[L]] = pre[L+1];
dfs(L+1,R,inL,inR-1);
}else{
int len = pos[pre[L]]-inL;
left_c[pre[L]] = pre[L+1];
right_c[pre[L]] = pre[L+len+1];
dfs(L+1,L+len,inL,inL+len-1);
dfs(L+len+1,R,inL+len+1,inR);
}
}
void dfs2(int root){
if(!left_c[root]&&!right_c[root]){
re.push_back(root);
return;
}
if(left_c[root])
dfs2(left_c[root]);
if(right_c[root])
dfs2(right_c[root]);
re.push_back(root);
}
int main(){
cin>>N;
for(int i=0;i<2*N;i++){
cin>>type;
if(type=="Push"){
cin>>num;
pre[pre_num] = num;
pre_num++;
stk.push(num);
}
else{
pop_seq[pop_num] = stk.top();
pos[stk.top()] = pop_num;
stk.pop();
pop_num++;
}
}
dfs(1,N,1,N);
dfs2(pre[1]);
for(int i=0;i<re.size();i++){
if(i!=0) cout<<" ";
cout<<re[i];
}
}
4. All Roads Lead to Rome
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers N (2≤N≤200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N−1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format City1 City2 Cost. Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.
Output Specification:
For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommanded. If such a route is still not unique, then we output the one with the maximum average happiness – it is guaranteed by the judge that such a solution exists and is unique.
Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommanded route. Then in the next line, you are supposed to print the route in the format City1->City2->…->ROM.
Sample Input:
6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1
Sample Output:
3 3 195 97
HZH->PRS->ROM
我用的多标尺迪杰斯特拉,写的比较复杂,有简洁得多的代码。
最容易出错的是第一个输出数字。我们用tot[]表示每个点目前找到了多少条最短路径,如果最近距离更新,就等于上一个点的tot,如果相等,就加上上一个点的tot,表示加上来自那个方向的最短路径数。
#include<bits/stdc++.h>
using namespace std;
struct edge{
int to;
int w;
int happy;
edge(int a,int b,int c){
to=a;
w=b;
happy = c;
}
};
struct node{
int id;
int n_dis;
int happy;
node(int a,int b,int c){
id=a;
n_dis=b;
happy=c;
}
bool operator<(const node &b)const{
if(n_dis!=b.n_dis)
return n_dis>b.n_dis;
else return happy<b.happy;
}
};
vector<edge> vec[501];
multiset<int> temp;
int dis[501];
int happy[501];
int tot_happy[501];
int tot[501];
string name[501];
int vis[501];
int fa[501];
int path_len[501];
int N,M,hape;
string start,tool,from,to;
unordered_map<string,int> mp;
double pre_avg;
void diji(int s){
priority_queue<node> q;
memset(dis,0x3f3f3f3f,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(tot_happy,0,sizeof(tot_happy));
memset(tot,0,sizeof(tot));
memset(fa,-1,sizeof(fa));
dis[s] = 0;
tot[s] = 1;
tot_happy[s] = 0;
q.push(node(s,0,0));
while(!q.empty()){
node now = q.top();
q.pop();
if(vis[now.id]) continue;
vis[now.id] = 1;
for(int i=0; i<vec[now.id].size();i++){
if(vis[vec[now.id][i].to]) continue;
if(dis[vec[now.id][i].to]>dis[now.id]+vec[now.id][i].w){
tot[vec[now.id][i].to] = tot[now.id];
fa[vec[now.id][i].to] = now.id;
tot_happy[vec[now.id][i].to] = tot_happy[now.id]+vec[now.id][i].happy;
dis[vec[now.id][i].to] = dis[now.id]+vec[now.id][i].w;
q.push(node(vec[now.id][i].to,dis[vec[now.id][i].to],tot_happy[vec[now.id][i].to]));
//cout<<name[vec[now.id][i].to]<<"进入"<<endl;
if(vec[now.id][i].to==mp["ROM"]){
int gg = now.id;
int tot_num = 0;
while(gg!=0){
gg = fa[gg];
tot_num++;
}
pre_avg = (tot_happy[now.id]*1.0)/(tot_num*1.0);
}
}
else if(dis[vec[now.id][i].to]==dis[now.id]+vec[now.id][i].w){
tot[vec[now.id][i].to] += tot[now.id];
if(tot_happy[vec[now.id][i].to]<tot_happy[now.id]+vec[now.id][i].happy){
fa[vec[now.id][i].to] = now.id;
tot_happy[vec[now.id][i].to] = tot_happy[now.id]+vec[now.id][i].happy;
dis[vec[now.id][i].to] = dis[now.id]+vec[now.id][i].w;
q.push(node(vec[now.id][i].to,dis[vec[now.id][i].to],tot_happy[vec[now.id][i].to]));
}
else if(tot_happy[vec[now.id][i].to]==tot_happy[now.id]+vec[now.id][i].happy){
int gg = now.id;
int tot_num = 0;
while(gg!=0){
gg = fa[gg];
tot_num++;
}
double avg2 = (tot_happy[now.id]*1.0+vec[now.id][i].happy)/((tot_num+1)*1.0);
if(avg2>pre_avg){
fa[vec[now.id][i].to] = now.id;
tot_happy[vec[now.id][i].to] = tot_happy[now.id]+vec[now.id][i].happy;
dis[vec[now.id][i].to] = dis[now.id]+vec[now.id][i].w;
q.push(node(vec[now.id][i].to,dis[vec[now.id][i].to],tot_happy[vec[now.id][i].to]));
}
}
//cout<<name[vec[now.id][i].to]<<" "<<name[now.id]<<endl;
}
}
}
}
int main(){
int len;
ios::sync_with_stdio(false);
cin>>N>>M>>start;
name[0] = start;
mp[start] = 0;
happy[0] = 0;
for(int i=1;i<N;i++){
cin>>tool>>hape;
happy[i] = hape;
name[i] = tool;
mp[tool] = i;
}
while(M--){
cin>>from>>to>>len;
vec[mp[from]].push_back(edge(mp[to],len,happy[mp[to]]));
vec[mp[to]].push_back(edge(mp[from],len,happy[mp[from]]));
}
diji(0);
stack<int> stack_len;
vector<int> path_len;
int now = mp["ROM"];
while(now!=-1){
stack_len.push(now);
now = fa[now];
}
while(!stack_len.empty()){
path_len.push_back(stack_len.top());
stack_len.pop();
}
cout<<tot[mp["ROM"]]<<" "<<dis[mp["ROM"]]<<" "<<tot_happy[mp["ROM"]]<<" "<<tot_happy[mp["ROM"]]/(path_len.size()-1)<<endl;
for(int i=0;i<path_len.size();i++){
if(i!=0) cout<<"->";
cout<<name[path_len[i]];
}
}