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个字符串; 每次操作,你可以把字符串的每个元素整体左移(最左边那个字符跑到最后面去 ...
随机推荐
- VMware Playerでの仮想マシン起動エラー
Windows Updateすると.翌日VMware Playerの仮想マシン起動時に 「この仮想マシンを構成済み設定でパワーオンするのに十分な物理メモリがありません.」 のエラーとなることが時々あり ...
- 线程同步 – lock和Monitor
在多线程代码中,多个线程可能会访问一些公共的资源(变量.方法逻辑等等),这些公共资源称为临界区(共享区):临界区的资源是不安全,所以需要通过线程同步对多个访问临界区的线程进行控制. 同样,有些时候我们 ...
- iOS开发-iOS7禁用手势返回
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // 禁用 iOS7 返回手势 if ([self.nav ...
- sutdio中替换全局方法
Ctril Shift R Text to find :是要搜索的内容或者要被替换的内容 Replace with :是要替换的内容 Preview:是可以看到预览 在Scope的选项卡里 Whole ...
- 51单片机的idata,xdata,pdata,data的详解(转)
data: 固定指前面0x00-0x7f的128个RAM,可以用acc直接读写的,速度最快,生成的代码也最小. bit :是指0x20-0x2f的可位寻址区idata:固定指前面0x00-0xff的2 ...
- 简析iOS动画原理及实现——Core Animation
本文转载至 http://www.tuicool.com/articles/e2qaYjA 原文 https://tech.imdada.cn/2016/06/21/ios-core-animati ...
- iOS - 选取相册中iCloud云上图片和视频的处理
关于iOS选取相册中iCloud云上图片和视频 推荐看:TZImagePickerController的源码,这个是一个非常靠谱的相册选择图片视频的库 .当然也可以自己写 如下遇到的问题 工作原因, ...
- 非IMU模式下DML语句产生的REDO日志内容格式解读
实验内容:非IMU模式下DML语句产生的REDO日志内容格式解读 最详细的解读是UPDATE的. 实验环境准备 11G中默认是开启IMU特性的,做此实验需要关闭此特性. alter system se ...
- android第三方---->android智能机器人的使用
在网上找了个第三方智能机器人,可以实现聊天语音等功能,比较不错的.今天我们就开始智能机器人聊天的学习,例子中涉及的handler的有关知识和json数据的解析,请参见我的博客:android基础--- ...
- Python 2.7.6 安装lxml模块[ubuntu14.04 LTS]
lxml --->首字母是字母l,不是数字1 lxml 2.x : https://pypi.python.org/pypi/lxml/2.3 1xml官网:http://lxml.de/ 一 ...