当前位置:网站首页>Codeforces Round #797 (Div. 3) F. Shifting String题解
Codeforces Round #797 (Div. 3) F. Shifting String题解
2022-06-21 11:48:00 【黄二十九】
题意是给一个字符串s和一个与之相同长度的数组排列a,字符串s按照数组给定的位置进行变化,新字符串的第i个字符是上一个字符串的第a[i]个字符
比如 例子
5
ababa
2 1 4 5 3
变化6次
babaa
abaab
baaba
abbaa
baaab
ababa
变回原来的字符串,问需要最少几次变化为原来的字符串
第一个点,观察发现,字符串变化时一些位置的字符变化时循环的,比如上例的ababa的前两个字符ab,因为a[1]=2,a[2]=1,第一个字符到第二个字符,第二个字符到第一个字符,如此一直循环。因此可以建图,从结点 i 连一条边到结点 a[i] ,可以得出一个结论,所有这种图上的环都可以到变回到原来的样子,这是第一个点,应该不难发现
第二个点,这种环需要几次变化才能回到原来状态呢,开始我想当然以为如果环上字符都相等是一次,否则为环上结点的个数,直接wa了,后来想了一下应该是字符串的子字符串循环的最小长度
比如abcabc是3,因为变化3次为bcabca,cabcab,abcabc
第三个点,知道了每个环上变换的次数,那么总次数是多少呢,我又想当然的以为是全部乘起来,然后又wa了,其实想了一下应该是每个环的变换次数的最小公倍数(我好笨......),因为最小公倍数能保证每个环的循环的次数的倍数,让其回到原来的状态,所以写个lcm就可以过了
一个1700的比较综合的题目,记录一下,wa了两发才过,刚放暑假好久没写代码手都生了,看来还得再仔细一点
以下为AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[210];
int g[210];
bool used[210];
ll gcd(ll a,ll b)
{
return b?gcd(b,a%b):a;
}
ll lcm(ll a,ll b)
{
return a*b/gcd(a,b);
}//求最小公倍数
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
scanf("%d%s",&n,s+1);
for(int i=1;i<=n;i++)
{
used[i]=false;
int k;
cin>>k;
g[i]=k;
}
ll ans=1;
for(int i=1;i<=n;i++)
{
if(!used[i])
{
int nodenum=0;
string tmp="";
int loc=i;
while(!used[loc])//把环上字符连成串
{
used[loc]=true;
tmp+=s[loc];
loc=g[loc];
}
int sl=tmp.length();
for(int i=1;i<=sl;i++)//求变化最小次数
{
if(sl%i==0)
{
bool flag=true;
for(int j=0;j<i;j++)
{
char pre=tmp[j];
for(int k=j;k<sl;k+=i)
{
if(pre!=tmp[k])
{
flag=false;
break;
}
}
if(!flag) break;
}
if(flag)
{
nodenum=i;
break;
}
}
}
ans=lcm(ans,(ll)nodenum);
}
}
cout<<ans<<"\n";
}
return 0;
}边栏推荐
- 第八章 Web项目测试
- 矩形覆盖面积
- Customization of power aging test system | overview of charging pile automatic test system nsat-8000
- One's deceased father grind politics English average cent furnace! What is your current level?
- What is the relationship between CSC securities and qiniu school? Is it safe to open a brokerage account
- 2022 special operation certificate examination question bank and online simulation examination for safety management personnel of hazardous chemical business units
- Devsecops: s-sdlc enterprise best practices
- 站在数字化风口,工装企业如何“飞起来”
- 【100个 Unity踩坑小知识点】| Unity控制物体持续指向某个方向
- 服务器安全审计系统设计与实现
猜你喜欢

Heavyweight, mapstruct 1.5 was released. This time, it finally supports the transformation of map into bean!

Qmlbook learning summary

Break down tasks

1108. IP 地址无效化

Introduction to the upper computer software ns-scope of Tektronix oscilloscope

【yolov5s目标检测】opencv加载onnx模型在GPU上进行推理

有意思的鼠标指针交互探究

Is 100W data table faster than 1000W data table query?
Golang implements redis (9): use geohash to search people nearby

IMU选型、标定误差分析、AHRS组合导航
随机推荐
Devsecops: s-sdlc enterprise best practices
Harmonyos training I
It is the German oscilloscope software and the keysight oscilloscope upper computer software ns-scope
Is the Huatai Securities account given by qiniu school true? Is it safe to open an account
Deep water area involvement
【100个 Unity踩坑小知识点】| Unity中的 碰撞盒检测 Physics.OverlapBox、OverlapCaps
matrial3d参数分析
完美安全代码审计的5个最佳实践
Matrial3d parameter analysis
DDoS attack and defense: from principle to practice
What if the server is invaded
南京大学 静态软件分析(static program analyzes)-- introduction 学习笔记
Qmlbook learning summary
typora免费版,无需破解,安装直接使用
使用Huggingface在矩池云快速加载预训练模型和数据集
Break down tasks
图文并茂--微信小程序,获取用户地理位置信息,并调用腾讯地图API来获取用户具体位置
【100个 Unity实用技能】| 游戏中使技能或装备跟随角色环绕,持续旋转
A complete open source Internet of things basic platform
旅行不能治愈心灵