Codeforces 798 B. Mike and strings-String的find()函数
好久好久好久之前的一个题,今天翻cf,发现这个题没过,补一下。
2 seconds
256 megabytes
standard input
standard output
Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec".
Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?
The first line contains integer n (1 ≤ n ≤ 50) — the number of strings.
This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.
Print the minimal number of moves Mike needs in order to make all the strings equal or print - 1 if there is no solution.
4
xzzwo
zwoxz
zzwox
xzzwo
5
2
molzv
lzvmo
2
3
kc
kc
kc
0
3
aa
aa
ab
-1
In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".
题意:给定的n个串,最少能移动几次使得这n个字符串相等。每次只能将字符串首位的移动到末尾。
想法:以某个串为标准, 改变其他的字符串,找出变化次数最小的。
这个题用string中的find()函数,简直不要太炫酷。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=+;
string a[N]; int main(){
int n,i,j;
while(~scanf("%d",&n)){
for(i=;i<n;i++)
cin>>a[i];
int ans=0x3f3f3f,flag=;
for(i=;i<n;i++){
int sum=;
for(j=;j<n;j++){
string tmp=a[j]+a[j];
if(tmp.find(a[i])==string::npos)flag=;
else sum+=tmp.find(a[i]);
}
ans=min(ans,sum);
}
printf("%d\n",flag?-:ans);
}
return ;
}
string中的find()函数,查找字符串A是否包含子串B,不是用strA.find(strB) > 0而是strA.find(strB) != string:npos
返回的值是B在A中第一次出现的下标位置,如果没有查询到,则返回string::npos,如果返回npos就输出-1。
关于string中的find函数,看其他人写的吧。
备忘一下。
Codeforces 798 B. Mike and strings-String的find()函数的更多相关文章
- 【codeforces 798B】Mike and strings
[题目链接]:http://codeforces.com/contest/798/problem/B [题意] 给你n个字符串; 每次操作,你可以把字符串的每个元素整体左移(最左边那个字符跑到最后面去 ...
- codeforces 798 D. Mike and distribution(贪心+思维)
题目链接:http://codeforces.com/contest/798/problem/D 题意:给出两串长度为n的数组a,b,然后要求长度小于等于n/2+1的p数组是的以p为下表a1-ap的和 ...
- codeforces 798 C. Mike and gcd problem(贪心+思维+数论)
题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...
- codeforces 798 D. Mike and distribution
D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 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个串,每次操作可以将 ...
- Mike and strings 798B
B. Mike and strings time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- Codeforces 196 D. The Next Good String
D. The Next Good String time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- js模板引擎之 Handlebars 基本用法
模板引擎比较久远的一个技术,常见的模板引擎有 baiduTemplate(百度)\artTemplate(腾讯)\juicer(淘宝)\doT\ tmpl\ handlebars\ easyTempl ...
- python学习笔记一:数据类型
一.Python文件类型 1.源代码 hello.py: 1 #!/usr/bin/python 2 print "hello world" 2.字节代码:python源文件经编译 ...
- leetcode 【 Linked List Cycle 】 python 实现
题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ...
- python中subprocess.Popen执行命令并持续获取返回值
先举一个Android查询连接设备的命令来看看Python中subprocess.Popen怎么样的写法.用到的命令为 adb devices. import subprocess order='ad ...
- Java 中的 I/O 抽象
Table of Contents 前言 字节流 常用实现 字符流 常用实现 缓冲区 各种字节流 结语 前言 由于在接触 Java 之前使用的语言是 Python,所以在转到 Java 后对 Java ...
- Python学习-day19 django基础篇
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...
- [Ceres]C++优化库
官网教程: http://ceres-solver.org/nnls_tutorial.html 定义了一个最小二乘法求解器 自动求导的功能
- python XlsxWriter创建Excel 表格
文档(英文) https://xlsxwriter.readthedocs.io/index.html 常用模块说明(中文) https://blog.csdn.net/sinat_35930259/ ...
- HDU 4101 Ali and Baba (思路好题)
与其说这是个博弈,倒不如说是个搜索.这题思路不错,感觉很难把情况考虑周全. 在地图外围填充一圈0,两次BFS,第一次从-1点出发,把从-1到达的0点以及包围0的那一圈石头标记出来.如下图: 1 1 1 ...
- shell sort 排序大讨论
转自http://roclinux.cn 本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. === [正文开始]有时候学 ...