Codeforces Round #539 (Div. 2) - D. Sasha and One More Name(思维)
Problem Codeforces Round #539 (Div. 2) - D. Sasha and One More Name
Time Limit: 1000 mSec
Problem Description
Input
The first line contains one string s (1≤|s|≤5000) — the initial name, which consists only of lowercase Latin letters. It is guaranteed that s is a palindrome.
Output
Print one integer k — the minimum number of cuts needed to get a new name, or "Impossible" (without quotes).
Sample Input
nolon
Sample Output
2
题解:这个题有个很良心的样例,就是"qqqq"的结果是无解,看到这个样例的第一反应就是如果有两个不同的字符就肯定可以(这里有点小问题),并且至多两次就可以构造出新的回文串,其实如果熟悉这种题目的话到这里就结束了,接下的工作就是验证能不能只剪一次,根据s的长度可知O(n^2)的复杂度是完全可以接受的,枚举在哪剪开,再进行判断即可,刚才有点问题的说法的反例就是 aba,这种就是无解的情况,其实稍加改进即可,也就是判断前 [n/2]个字符是否只有一种字符,如果不是那么原始串必定形如
a...b...b...a
这样就不会无解,反之如果只有一种字符,对于长度为偶数的串,相当于整个串只有一种字符,对于长度为奇数的串,相当于aba的情况,都是无解的,判断完无解之后就只剩枚举判断是否能够只剪一次了。
- #include <bits/stdc++.h>
- using namespace std;
- #define REP(i, n) for (int i = 1; i <= (n); i++)
- #define sqr(x) ((x) * (x))
- const int maxn = + ;
- const int maxm = + ;
- const int maxs = + ;
- typedef long long LL;
- typedef pair<int, int> pii;
- typedef pair<double, double> pdd;
- const LL unit = 1LL;
- const int INF = 0x3f3f3f3f;
- const double eps = 1e-;
- const double inf = 1e15;
- const double pi = acos(-1.0);
- const int SIZE = + ;
- const LL MOD = ;
- string str;
- bool is_pd(string &s)
- {
- int len = s.size();
- for (int i = , j = len - ; i < j; i++, j--)
- {
- if (s[i] != s[j])
- return false;
- }
- return true;
- }
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie();
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- cin >> str;
- int len = str.size();
- bool ok = false;
- for (int i = ; i < len / ; i++)
- {
- if (str[i] != str[i - ])
- {
- ok = true;
- break;
- }
- }
- if (!ok)
- {
- cout << "Impossible" << endl;
- return ;
- }
- int ans = ;
- string tmp = "";
- for (int i = ; i < len; i++)
- {
- tmp += str[i - ];
- string tt = str.substr(i, len) + tmp;
- if (tt != str && is_pd(tt))
- {
- ans = ;
- break;
- }
- }
- cout << ans << endl;
- return ;
- }
Codeforces Round #539 (Div. 2) - D. Sasha and One More Name(思维)的更多相关文章
- Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax(思维题)
Problem Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax Time Limit: 2000 mSec Problem ...
- Codeforces Round #539 (Div. 2) C. Sasha and a Bit of Relax(前缀异或和)
转载自:https://blog.csdn.net/Charles_Zaqdt/article/details/87522917 题目链接:https://codeforces.com/contest ...
- Codeforces Round #539 (Div. 2) C Sasha and a Bit of Relax
题中意思显而易见,即求满足al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ar且l到r的区间长为偶数的这样的数对(l,r)的个数. 若al⊕al+1⊕…⊕amid=amid+1⊕amid ...
- Codeforces Round #539 (Div. 1) C. Sasha and a Patient Friend 动态开点线段树
题解看这里 liouzhou_101的博客园 更简洁的代码看这里: #include <bits/stdc++.h> using namespace std; typedef long l ...
- Codeforces Round #539 (Div. 1) E - Sasha and a Very Easy Test 线段树
如果mod是质数就好做了,但是做除法的时候对于合数mod可能没有逆元.所以就只有存一下mod的每个质因数(最多9个)的幂,和剩下一坨与mod互质的一部分.然后就能做了.有点恶心. CODE #incl ...
- Codeforces Round #539 (Div. 1) 1109F. Sasha and Algorithm of Silence's Sounds LCT+线段树 (two pointers)
题解请看 Felix-Lee的CSDN博客 写的很好,不过最后不用判断最小值是不是1,因为[i,i]只有一个点,一定满足条件,最小值一定是1. CODE 写完就A,刺激. #include <b ...
- Codeforces Round #539 (Div. 2)
Codeforces Round #539 (Div. 2) A - Sasha and His Trip #include<bits/stdc++.h> #include<iost ...
- Codeforces Round #539 (Div. 2) 题解
Codeforces Round #539 (Div. 2) 题目链接:https://codeforces.com/contest/1113 A. Sasha and His Trip 题意: n个 ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)
Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...
随机推荐
- c++右值引用以及使用
前几天看了一篇文章<4行代码看看右值引用> 觉得写得不错,但是觉得右值引用的内容还有很多可以去挖掘学习,所以总结了一下,希望能对右值引用有一个更加深层次的认识 一.几个基本概念 1.1左值 ...
- Oracle-02:SQL语言的分类或者说SQL语言的组成
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 小结一版: 01.DDL(Data Definition Language)数据定义语言. 用来创建数据库中 ...
- JS奇淫巧技:防抖函数与节流函数
应用场景 实际工作中,我们经常性的会通过监听某些事件完成对应的需求,比如: 通过监听 scroll 事件,检测滚动位置,根据滚动位置显示返回顶部按钮 通过监听 resize 事件,对某些自适应页面调整 ...
- Android 不规则图像填充 小玩着色游戏
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/45788433: 本文出自:[张鸿洋的博客] 一.概述 近期群里偶然看到一哥们在 ...
- 关于 行高lineheight的概念及与Vertical-align、内容框、基线等的关系
1.关于行高 行高:顾名思意指一行文字的高度.具体来说是指两行文字间基线之间的距离,他也是底线和顶线之间距离 (1)内容区
- BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树
BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树 Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为 ...
- C#多线程中的异常处理
常规Thread中处理异常 使用Thread创建的子线程,需要在委托中捕捉,无法在上下文线程中捕捉 static void Main(string[] args) { ThreadStart thre ...
- IP地址、端口、TCP协议、UDP协议
最近在看<疯狂java讲义>,第17章网络编程里提到IP地址.端口.TCP协议.UDP协议这几个概念.以前上课时学过,现在重温了一遍.在这里,用自己的语言简单的讲解一下吧. IP地址:每一 ...
- 【毕业原版】-《贝德福特大学毕业证书》Bedfordhire一模一样原件
☞贝德福特大学毕业证书[微/Q:865121257◆WeChat:CC6669834]UC毕业证书/联系人Alice[查看点击百度快照查看][留信网学历认证&博士&硕士&海归& ...
- 基于ZigBee模块与51单片机之间的简化智能家居项目简介(学生版本)
5月份学校举行比赛,我们团队报名<智能家居>的项目,设计的总体思路用:QT写的上位机与ZigBee无线通信加51作为终端的简易版智能家居 电路连接:PC机->cc2530(协调器)- ...