求教: 明明输出格式与示例相同但是是0分死活过不去QAQ
查看原帖
求教: 明明输出格式与示例相同但是是0分死活过不去QAQ
1500771
sssTNT楼主2024/11/11 17:31
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
  // 不知道为什么过不去, 求教QAQ
  int n, k = 0;
  cin >> n >> k;
  vector <string> variates;
  vector <int> values;

  while (n--) {
    string variate;
    cin >> variate;
    variates.push_back(variate);
    int value = 0;
    cin >> value;
    values.push_back(value);
  }

  while (getchar() != '\n'); // 当下面使用getline(cin, )时会先读取回车字符, 为防止输入数据丢失此处需提前将回车字符读取

  while (k--) {
    string sentence;
    getline(cin, sentence);
    int position_left = sentence.find("{");
    while (sentence.find("{") != string::npos) {
      int position_right = sentence.find("}");
      string get_variate = sentence.substr(position_left+1, position_right-position_left-1);
      int i = 0;
      for (; i < (int)sizeof(variates); i++) 		{
        if (variates[i] == get_variate)
          break;
      }
      int length = position_right-position_left+1;
      sentence.replace(position_left, length, to_string(values[i]));
      position_left = sentence.find("{", position_left+1);
    }
    cout << sentence << endl;
  }

  return 0;
}
2024/11/11 17:31
加载中...