进阶20:给出一个整数 n(n<10^30) 和 k 个变换规则规则:一位数可变换成另一个一位数,变换得到的数不能为零。仅要求输出经过任意次的变换产生出不同整数的个数。
例如:n=234。有规则(k=2):
2-> 5
3-> 6
上面的整数 234 经过变换后可能产生出的整数为(包括原数):
234
534
264
564
共 4 种不同的产生数
- 经过测试,每一位数字的变化能经过多次变化规则。
- DFS列出所有可能数字过于复杂且数据规模超过int的数量级。
- 列出数字形式是考虑到形成不同的数字才能计数。实际上每一位取到不同的数字得到的结果都是不同的,不需要形成数字后进行比较是否重复。
- 数据规模总次数超过int上限,用数组的大数乘和string接收字符串。
cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> orgin;
vector<int> target;
vector<int> ans;
vector<int> FinAns;
void GetNum(vector<int>& UsedNum, int cur) {
// 如果当前数字已经在 UsedNum 里,说明已经处理过,避免重复
if (find(UsedNum.begin(), UsedNum.end(), cur) != UsedNum.end()) {
return;
}
UsedNum.push_back(cur);
// 尝试所有规则,看 cur 能变成什么
for (int i = 0; i < orgin.size(); i++) {
if (orgin[i] == cur) {
//对新变化的数检查是否存在新转化规则
GetNum(UsedNum, target[i]);
}
}
}
void DFS(vector<int> numbers,int index,vector<int> UsedNum){
if(index==numbers.size()){
return;
}
//得到该位能取到的全部数字
GetNum(UsedNum,numbers[index]);
ans.push_back(UsedNum.size());
vector<int> newUsedNum;
DFS(numbers,index+1,newUsedNum);
return;
}
void DaShuCheng(int index,int& jin){
int num = ans[index];
if(index==0){
FinAns.push_back(num);
}
else{
for(int i=0;i<FinAns.size();i++){
FinAns[i]*=num;
FinAns[i]+=jin;
jin = FinAns[i]/10;
FinAns[i]%=10;
}
if(jin){
FinAns.push_back(jin);
jin=0;
}
}
}
int main(){
string num;
int len;
cin>>num>>len;
vector<int> numbers;
for(int i=0;i<num.size();i++){
numbers.push_back(num[i] - '0');
}
for(int i=0;i<len;i++){
int map1,map2;
cin>>map1>>map2;
orgin.push_back(map1);
target.push_back(map2);
}
vector<int> UsedNum;//记录index对应数字的变化形式
DFS(numbers,0,UsedNum);
int jin=0;
for(int i=0;i<ans.size();i++){
DaShuCheng(i,jin);
}
reverse(FinAns.begin(),FinAns.end());
for(int i=0;i<FinAns.size();i++){
cout<<FinAns[i];
}
system("pause");
}
进阶21:将念头从1到n编号,念头i来源于念头from[i],保证from[i]<i,from[i]=0表示该念头没有来源念头,只是脑袋一抽,灵光一现。输出共一行,一个正整数L表示最长的念头因果链中的念头数量
样例输入
8
0
1
0
3
2
4
2
4
样例输出
3
样例说明
最长的因果链有:
1->2->5 (from[5]=2,from[2]=1,from[1]=0)
1->2->7 (from[7]=2,from[2]=1,from[1]=0)
3->4->6 (from[6]=4,from[4]=3,from[3]=0)
3->4->8 (from[8]=4,from[4]=3,from[3]=0)
- 错误1:越界异常,产生死循环了,错误在于来源是编号1~n,而对应下标为1-1~n-1。将TraceBack(LianChang,newindex,from);改为TraceBack(LianChang,newindex-1,from);
cpp
#include <bits/stdc++.h>
using namespace std;
void TraceBack(int &LianChang,int index,vector<int>&from){
LianChang++;
if(from[index]==0){
return ;
}
else{
int newindex = from[index];
TraceBack(LianChang,newindex-1,from);
}
}
int main(){
int len;cin>>len;
vector<int> from(len);
for(int i=0;i<len;i++){
cin>>from[i];
}
int LianChang=0;
int ans=0;
for(int i=0;i<len;i++){
LianChang=0;
TraceBack(LianChang,i,from);
ans = max(ans,LianChang);
}
cout<<ans<<endl;
system("pause");
}
In recent years, pre-trained models have played an important role in artificial intelligence research. Researchers typically first train a general model using large-scale datasets and then fine-tune it on specific tasks. In this way, the model can utilize the knowledge learned during the pre-training stage to improve the performance of downstream tasks. For example, in the field of natural language processing, many language models are pre-trained on massive text corpora and achieve excellent results in tasks such as text classification, machine translation, and question answering. The pre-training and fine-tuning framework not only reduces training costs but also improves the generalization ability of models. Therefore, this approach has become an important paradigm in modern artificial intelligence research.
- 近年来,预训练模型在人工智能研究中扮演了重要的角色。研究员普遍地使用大规模数据集来先训练一个普遍的模型再在特定任务中调优。在这种方式里,模型能在预训练阶段利用学习到的知识来改善下游任务中的表现。比如,在自然语言处理领域中,许多语言模型再大量的文本语料库中预训练并在一些任务中取得了极好的结果,比如文本分类、机器翻译和回答问题。预训练和微调架构不仅减少了训练成本,还提高了模型的泛化能力。从而,这一方法在现代化的人工智能研究中成为了一个重要范例。
- downstream tasks 下游任务、 fine-tuning 微调