刚笔试完,C++方向,3道编程题,只AC出第二题,第三题写了一半,本地用embarcadero Dev-C++ debug的,环境不熟悉,思路都应该是对的,但没A出来。收题了,根据回忆继续debug的代码,记录一下。
题目前面忘记了,输入输出大致如下:
输入:
第一行:n 数组从度
第二行:第一个数组
第三行:第二个数组
输出:
存储次数
例子:
输入:
3
1 2 3
2 3 1
输出:
8
说明:
1 2 3 12 23 123 31 231 总共8中不重复的存储段
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main() {
int n;
cin >> n;
int res = 0;
vector<vector<int>> input;
string str[2];
for (int i = 0; i < 2; i++) { //get input
string tmp;
//vector<int> a;
for (int j = 0; j < n; j++) {
cin >> tmp;
str[i] = str[i] + tmp;
//a.push_back(tmp);
}
//input.push_back(a);
}
cout << str[0] << " "<<str[1] << endl;
vector<string> list;
for (int i = 0; i < 2; i++) { //get sub string
string str_tmp;
for (int j = 0; j < n; j++)
{
for(int p = 1;p < n-j+1; p++)
{
str_tmp = str[i].substr(j,p);
//cout << str_tmp << " ";
list.push_back(str_tmp);
}
}
cout << endl;
}
sort(list.begin(),list.end()); //sort the vector
list.erase(unique(list.begin(),list.end()),list.end()); //get unique sub string
res = list.size(); //get result = number of unique substring
cout << res << endl;
return 0;
}