codeforces 798B - Mike and strings
感觉自己好咸鱼呀……B题写了这么久,虽然可以算作1A(忽略一次少include一个头文件的CE)……
思想很简单,每次选定一个字符串作为目标字符串,然后把其他所有字符串都当做测试字符串,计算出总共需要的步数,再在这些计算出的步数中找到最小的那个就是答案。
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char str[][];
int n;
bool is_equal(char sta[],char tes[],int len)//判断两个字符串是否相等
{
for(int i=;i<len;i++)
{
if(sta[i]!=tes[i]) return false;
}
return true;
}
int step(int sta,int tes)//计算从tes测试字符串到sta目标字符串需要多少步
{
int cnt=;
int len=strlen(str[sta]);
char test[*len+];//新建一个test字符串
for(int i=;i<len;i++) test[i]=str[tes][i];
for(int i=;i<len;i++) test[len+i]=str[tes][i];
test[*len]='\0';
//以上三行是做如此的工作:源字符串:“abc”,test字符串:“abcabc”,这样可以方便后续计算步数
while(cnt<len)//计算出从tes测试字符串到sta目标字符串需要cnt步
{
if(is_equal(str[sta],test+cnt,len)) break;
else cnt++;
}
if(cnt>=len) return -;//如果跑完一圈依然没有能够找到某次和目标字符串相同,那么就代表ans=-1;
return cnt;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%s",str[i]);
int ans=,flag=;
for(int i=;i<=n;i++)//枚举目标字符串
{
int cnt=;
for(int j=;j<=n;j++)//枚举测试字符串
{
if(i!=j)
{
int tmp=step(i,j);
if(tmp==-){
printf("-1\n");
return ;
}
else cnt+=tmp;
}
}
if(cnt<ans) ans=cnt;
}
printf("%d\n",ans);
}
codeforces 798B - Mike and strings的更多相关文章
- Mike and strings 798B
B. Mike and strings time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #410 (Div. 2) B. Mike and strings
B. Mike and strings time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CF410div2 B. Mike and strings
/* CF410div2 B. Mike and strings http://codeforces.com/contest/798/problem/B 字符串 暴力 题意:给你n个串,每次操作可以将 ...
- hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)
hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...
- Codeforces 385B Bear and Strings
题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...
- codeforces 547E Mike and Friends
codeforces 547E Mike and Friends 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define ...
- Codeforces 482C Game with Strings(dp+概率)
题目链接:Codeforces 482C Game with Strings 题目大意:给定N个字符串,如今从中选定一个字符串为答案串,你不知道答案串是哪个.可是能够通过询问来确定, 每次询问一个位置 ...
- codeforces 689 Mike and Shortcuts(最短路)
codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...
- 【codeforces 798B】Mike and strings
[题目链接]:http://codeforces.com/contest/798/problem/B [题意] 给你n个字符串; 每次操作,你可以把字符串的每个元素整体左移(最左边那个字符跑到最后面去 ...
随机推荐
- RedHat6.5-Linux安装telnet服务
1 下载以下三个包 telnet-0.17-47.el6.x86_64.rpm(telnet客户端) telnet-server-0.17-47.el6.x86_64.rpm(telnet服务端) x ...
- MySQL---insert into select from
INSERT INTO perf_week(node_id,perf_time,pm25,pm10,temp,humi) SELECT node_id,'2016-12-22 11:55:00' AS ...
- Go并发编程(四)
并发基础 多进程 多线程 基于回调的非阻塞/异步IO 协程 协程 与传统的系统级线程和进程相比,协程的最大优势在于其“轻量级”,可以轻松创建上百万个而不会导致系统资源衰竭, ...
- RESTful状态码说明
https://www.zhihu.com/question/58686782/answer/159603453 常用状态码: 200 请求成功并返回所需资源 400 客户端请求有语法错误 401 未 ...
- /usr/bin/ld: cannot find -lxxx 的解决办法
/usr/bin/ld: cannot find -lxxx 的解决办法 在软件编译过程中,经常会碰到类似这样的编译错误: /usr/bin/ld: cannot find -lhdf5 这表示找不到 ...
- Spring容器AOP的理解
一句话理解:根据被代理对象信息通过Proxy动态生成我们具体的代理类. 实现就动态代理.那动态代理是什么呢? 动态代理其实并不是什么新鲜的东西,学过设计模式的人都应该知道代理模式,代理模式就是一种静态 ...
- 【代码审计】CLTPHP_v5.5.3后台任意文件下载漏洞分析
0x00 环境准备 CLTPHP官网:http://www.cltphp.com 网站源码版本:CLTPHP内容管理系统5.5.3版本 程序源码下载:https://gitee.com/chich ...
- JSON XSS
漏洞实例一: 1.在更新用户信息,修改联系电话,抓包绕过前端20个字符限制,Payload为 111<img src=1 onerror=alert(1)> 2.更新后,访问json 3. ...
- iOS开发--tarBarItem右上方显示badgeValue
直接设置tabBarItem.badgeValue没有效果,找原因半天发现ViewController被NavigationViewController包着 需这样设置才行: self.navigat ...
- 传入的结构体指针强制转为实例化结构体*v
struct val *v = (struct val *)arg;//传入的结构体指针强制转为实例化结构体*v struct val{ int num1; int num2; }; void *te ...