携程三道编程题(前两题)2020/09/08

第一题在这里插入图片描述

C++解法:

作者:没有人比我更懂高并发
链接:https://www.nowcoder.com/discuss/503503?type=post&order=time&pos=&page=0&channel=666&source_id=search_post
来源:牛客网

int main()
{
    
    
  string s,word,tihuan;
  cin>>s;
  getchar();
  getline(cin,word); 
  cin>>tihuan;
  vector<string>part;
 
  int i=0;
  string temp;
  while(i<word.size())
  {
    
    
    if(word[i]==' ' || word[i]==',')
    {
    
    
      part.push_back(temp);
      temp="";
      if(word[i]==',')
      {
    
    
        string g=",";
        part.push_back(g);
      }
    }
    else 
    {
    
    
      temp.push_back(word[i]);
    }
    i++;
  }
  part.push_back(temp);
  sort(s.begin(),s.end());
  for(int i=0;i<part.size();i++)
  {
    
    
    string temp=part[i];
    sort(temp.begin(),temp.end());
    if(temp==s)
    part[i]=tihuan;
  }
  string result="";
  for(int i=0;i<part.size();i++)
  if(part[i]==",")
  {
    
    
    result.pop_back();
    result=result+part[i];
  }
  else 
  result=result+part[i]+' ';
  cout<<result<<endl;
}
  

java解法:

作者:Duncan_dsf
链接:https://www.nowcoder.com/discuss/503417?type=0&order=0&pos=10&page=2&channel=666&source_id=discuss_center_0
来源:牛客网

public class P1 {
    
    

    public static void main(String[] args) {
    
    

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
    
    
            String target = scanner.nextLine(), line = scanner.nextLine(), rep = scanner.nextLine();
            HashMap<Character, Integer> map = new HashMap<>(64), temp = new HashMap<>(64);

            for (int i=0; i<target.length(); i++) {
    
    
                char ch = target.charAt(i);
                map.put(ch, map.getOrDefault(ch, 0)+1);
            }
            char[] array = line.toCharArray();
            StringBuilder sb = new StringBuilder();
            for (int i=0; i<array.length; ) {
    
    
                if (!isLetter(array[i])) {
    
    
                    sb.append(array[i]);
                    i++;
                } else {
    
    
                    int remain = target.length();
                    temp.clear();
                    temp.putAll(map);
                    int j;
                    for (j=i; remain > 0 && j<array.length && isLetter(array[j]); remain--, j++) {
    
    
                        char c = array[j];
                        if (temp.get(c) == null || c == 0)
                            break;
                        temp.put(c, map.get(c)-1);
                    }
                    if (remain == 0 && (j == array.length || !isLetter(array[j]))) {
    
    
                        sb.append(rep);
                        i = j;
                    } else {
    
    
                        for (; i<array.length && isLetter(array[i]); i++) {
    
    
                            sb.append(array[i]);
                        }
                    }
                }
            }
            System.out.println(sb.toString());
        }
    }

    public static boolean isLetter(char c) {
    
    
        return c>='a' && c<='z';
    }
}

第二题在这里插入图片描述

作者:Duncan_dsf
链接:https://www.nowcoder.com/discuss/503417?type=0&order=0&pos=10&page=2&channel=666&source_id=discuss_center_0
来源:牛客网

public class P2 {
    
    

    static LinkedList<String> res = new LinkedList<>();
    static LinkedList<Boolean> flag = new LinkedList<>();
    static StringBuilder temp = new StringBuilder();
    static HashMap<Character, Integer> map = new HashMap<>();
    static String[] arr;
    static int n;
    public static void main(String[] args) {
    
    

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
    
    

            String line = scanner.nextLine();
            arr = line.split(" ");
            n = arr.length;
            dfs(0, true);
            for (; res.size()>0; ) {
    
    
                System.out.print(res.removeFirst());
                if (!flag.removeFirst()) {
    
    
                    System.out.println("--circular dependency");
                } else {
    
    
                    System.out.println();
                }
            }
        }
    }

    public static void dfs(int cur, boolean f) {
    
    

        if (cur == n) {
    
    
            res.add(temp.toString());
            flag.add(f);
            return;
        }
        String str = arr[cur];
        for (int i = 0; i< str.length(); i++) {
    
    

            boolean tf = f;
            char ch = str.charAt(i);
            Integer c;
            if ((c = map.get(ch)) != null && c > 0) {
    
    
                tf = false;
            }
            map.put(ch, c==null ? 1 : c+1);
            temp.append(ch);

            dfs(cur+1, tf);
            temp.deleteCharAt(temp.length()-1);
            map.put(ch, c);
        }
    }
}

C++解法:

作者:没有人比我更懂高并发
链接:https://www.nowcoder.com/discuss/503503?type=post&order=time&pos=&page=0&channel=666&source_id=search_post
来源:牛客网

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
//回溯
vector<string>result;
string temp="";
void backward(vector<string>part,int index)
{
    
    
  if(temp.size()==part.size())
    result.push_back(temp);
  else if(temp.size()<part.size())
  {
    
    
    for(int i=0;i<part[index].size();i++)
    {
    
    
      temp.push_back(part[index][i]);
      backward(part,index+1);
      temp.pop_back();
    }
  }
}
int main()
{
    
    
  string s,cache;
  getline(cin,s);
  vector<string>part;
  int i=0;
  //切割字符
  while(i<s.size())
  {
    
    
    if(s[i]==' ')
    {
    
    
      part.push_back(cache);
      cache="";
    }
    else 
      cache.push_back(s[i]);
    i++;
  }
  part.push_back(cache);
 
  //回溯
  for(int i=0;i<part[0].size();i++)//start
  {
    
    
    temp.push_back(part[0][i]);
    backward(part,1);
    temp.pop_back();
  }
  //處理回路
  vector<int>flag(result.size(),0);
  for(int i=0;i<result.size();i++)
  {
    
    
    set<int>S;
    for(int j=0;j<result[i].size();j++)
    {
    
    
      if(S.find(result[i][j])==S.end())//not find
        S.insert(result[i][j]);
      else 
      {
    
    
        flag[i]=1;
        break;
      }
    }
  }
  for(int i=0;i<result.size();i++)
    if(flag[i])
      cout<<result[i]+"--circular dependency"<<endl;
    else cout<<result[i]<<endl;
}

#第三题
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41896265/article/details/108477517