B3859 [语言月赛 202309] 真的没有 idea 啦 题解

· · 题解

所有新手向我看齐。

题面

简化后:取 s 的前缀和后缀,使得两段的长度之和等于 t 的长度。将后缀放在前面,问符合新的字符串等于 t

思路

数据范围很小,模拟管够。那么,直接枚举,枚举前缀取多长,直接计算后缀应有的长度。取出来,计算答案。

代码

#include <iostream>

using namespace std;

string a, b;

int main() {
  int t;
  ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0); //读入优化。
  for (cin >> t; t; t--) {
    int ans = 0;
    cin >> a >> b;
    a = " " + a, b = " " + b; //塞一个空格方便处理。
    for (int i = 1; i < b.size() - 1; i++) { //枚举第一段的长度。
      string s1 = a.substr(1, i), s2 = a.substr(a.size() - b.size() + i + 1); //计算并取出, substr的第一个参数是起始位置,第二个是长度,第二个substr因为直接取到末尾,可以忽略长度。
      ans += (" " + s2 + s1 == b); //如果等于返回 1,否则返回 0(记得加空格)。
    }
    cout << ans << '\n';
  }
  return 0;
}

感谢各位观看。