Codeforces Round #410 (Div. 2)-B. Mike and strings-string的使用和取模

33 篇文章 0 订阅

给定一些字符串,每次把对头的放在最后,问需要多少操作才能让他们相等
开始以为是贪心,或者感觉无处可贪,应该是暴力。
题解中大概有三个版本。
第一个最简单的,因为运用了stl中的find函数,所以比较简单了
因为要涉及循环比较,所以每个串都加了一倍的长度。
然后直接find就好。
两个代码的整体实现方法都一样,但是用的手段不一样。
一个是用的stl(棒棒哒)
另一个是用的取模的思想和一个小技巧(66哒)
http://codeforces.com/contest/798/my

#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>

const int maxn=200;
using namespace std;
string m[maxn];
int main()
{   std::ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
   cin>>n;
    for(int i=1;i<=n;i++)
         cin>>m[i];
         string tmp;
         int cnt;
        int sum=0x3f3f3f3f;
       for(int i=1;i<=n;i++)
       {   cnt=0;//
           for(int j=1;j<=n;j++)
           {   tmp=m[j]+m[j];
               if(tmp.find(m[i])==string::npos)
               {  cout<<"-1"<<endl;
                  return 0;
               }
              cnt+=tmp.find(m[i]);

           }
           sum=min(cnt,sum);
       }
    cout<<sum<<endl;
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MAXN = 100;
string s[MAXN];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for(int i=0;i<n;i++)
        cin>>s[i];
    int m = s[0].length();
    int ans = INF;
    for(int k=0;k<m;k++)
    {
        int t = 0;
        for(int i=0;i<n;i++)
        {
            bool good = false;
            for(int j=0;j<m;j++)
            {
                bool ok = true;
                for(int g=0;g<m;g++)
                {
                    ok &= (s[0][(k+g)%m] == s[i][(j+g)%m]);
                }
                if(ok)
                {
                    good = true;
                    t += j;
                    break;
                }
            }
            if(!good)
            {
                cout << -1 << endl;
                return 0;
            }
        }
        ans=min(ans,t);
    }
    cout << ans << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值