CF1109B Sasha and One More Name
CF1109B Sasha and One More Name
- 构造类题目.仔细看样例解释能发现点东西?
- 结论:答案只可能是 \(Impossible,1,2\) .
- \(Impossible:\) 有 \(n\) 个或 \(n-1\) 个相同的字母,显然无法拼出另一个回文串.(样例3)
- \(1:\) \(Cut\) \(1\) 次,相当于是做了原串的一个循环排列. \(O(n^2)\) 对所有循环排列验证是否符合要求即可.(样例4)
- \(2:\) 在原串中找出一段 \(len<n/2\) 的前缀以及与它等长的后缀,将它们 \(Cut\) 出后交换.若所有的前缀与对应交换后都不符合要求,则一定是 \(Impossible\) 对应的两种局面,否则至少有一个前缀 \(pre\) 满足 \(pre!=inverse(pre)\),即它对应的 \(suf\) ,交换两者即得一个合法的解.(样例1)
- 只需判断是否为前 \(2\) 种情况,时间复杂度为 \(O(n^2)\) .
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
inline int read()
{
int x=0;
bool pos=1;
char ch=getchar();
for(;!isdigit(ch);ch=getchar())
if(ch=='-')
pos=0;
for(;isdigit(ch);ch=getchar())
x=x*10+ch-'0';
return pos?x:-x;
}
const int MAXN=5e3+10;
int n;
char s[MAXN];
int t[MAXN];
bool judge_imp()
{
for(int i=1;i<=n;++i)
if(++t[s[i]]>=n-1)
return true;
return false;
}
char curs[MAXN];
bool judge_palindrome()
{
for(int i=1;i<=n/2;++i)
if(curs[i]!=curs[n+1-i])
return false;
return true;
}
bool judge_unique()
{
for(int i=1;i<=n;++i)
if(curs[i]!=s[i])
return true;
return false;
}
bool judge_one()
{
for(int pos=2;pos<=n;++pos)// s[pos] is the first element
{
int p=0;
for(int i=pos;i<=n;++i)
curs[++p]=s[i];
for(int i=1;i<pos;++i)
curs[++p]=s[i];
if(judge_palindrome() && judge_unique())
return true;
}
return false;
}
int main()
{
scanf("%s",s+1);
n=strlen(s+1);
if(judge_imp())
return puts("Impossible")&0;
if(judge_one())
return puts("1")&0;
puts("2");
return 0;
}
CF1109B Sasha and One More Name的更多相关文章
- 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法
C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...
- codeforces 719E E. Sasha and Array(线段树)
题目链接: E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input sta ...
- Sasha and Array
Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input standard inp ...
- 【codeforces 718 C&D】C. Sasha and Array&D. Andrew and Chemistry
C. Sasha and Array 题目大意&题目链接: http://codeforces.com/problemset/problem/718/C 长度为n的正整数数列,有m次操作,$o ...
- 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 ...
- 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 ...
- CF719E. Sasha and Array [线段树维护矩阵]
CF719E. Sasha and Array 题意: 对长度为 n 的数列进行 m 次操作, 操作为: a[l..r] 每一项都加一个常数 C, 其中 0 ≤ C ≤ 10^9 求 F[a[l]]+ ...
- Codeforces 1109D Sasha and Interesting Fact from Graph Theory (看题解) 组合数学
Sasha and Interesting Fact from Graph Theory n 个 点形成 m 个有标号森林的方案数为 F(n, m) = m * n ^ {n - 1 - m} 然后就 ...
- CF1109D Sasha and Interesting Fact from Graph Theory
CF1109D Sasha and Interesting Fact from Graph Theory 这个 \(D\) 题比赛切掉的人基本上是 \(C\) 题的 \(5,6\) 倍...果然数学计 ...
随机推荐
- 关于java.lang.Exception:No tests found matching的一系列解决方法
问题描述: java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=yahaa], {ExactMatcher ...
- 解决VS2015中出现类似于error C4996: 'scanf': This function or variable may be unsafe的安全检查错误
用习惯了VS老版本的人当刚使用VS2013的时候可能总遇到类似于这样的错误: error C4996: 'scanf': This function or variable may be unsafe ...
- Servlet之监听事件细究
观察者三个模式: ServletContextListener:用于监听WEB 应用启动和销毁的事件,监听器类需要实现javax.servlet.ServletContextListener 接口. ...
- javascript的几种使用多行字符串的方式
JS里并没有标准的多行字符串的表示方法,但是在用模板的时候,为了保证模板的可阅读性,我们又不可避免的使用多行字符串,所以出现了各种搞法,这里以一段jade的模板作为示例,简单总结和对比一下. 字符串相 ...
- bzoj1007: [HNOI2008]水平可见直线 单调栈维护凸壳
在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的.例如,对于直线:L1:y=x; L2:y=-x; L3 ...
- rsa加解密密钥生成命令
(1)生成原始RSA私钥文件 rsa_private_key.pem(原始私钥) openssl genrsa -out rsa_private_key.pem 2048 (2)将原始RSA私钥转换为 ...
- day21 git & github + Celery 分布式任务队列
参考博客: git & github 快速入门http://www.cnblogs.com/alex3714/articles/5930846.html git@github.com:liyo ...
- linux---tar命令,vim编辑器,磁盘分区,挂载,链接
1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) [root@bogon ~]# cp /etc/passwd ./ [root@bogon ~]# cp /etc/ ...
- 解决在for循环内判断条件多次执行
最近遇到的这个问题,就是在for循环内if判断的条件会多次执行. 例如,在返回的30数据中,a条目是第7条则会进行30次判断,弹出29次查无数据,也就是要点击29次关闭alert,很是让人不爽. 有了 ...
- c# out ref 多个返回值问题个人总结
多个返回值可以用ref或者out来实现 如 var b=string.Empty(); var c=string.Empty(); public bool Test(string a, out str ...