codeforces gym 100971 K Palindromization 思路
2017-08-27 21:59
531 查看
题目链接:http://codeforces.com/gym/100971/problem/K
K. Palindromization
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output
Mihahim has a string s. He wants to delete exactly one character from it so that the resulting string would be a palindrome. Determine if he can do it, and if he can, what character should be deleted.
Input
The input contains a string s of length (2 ≤ |s| ≤ 200000), consisting of lowercase Latin letters.
Output
If the solution exists, output «YES» (without quotes) in the first line. Then in the second line output a single integer x — the number of the character that should be removed from s so that the resulting string would be a palindrome. The characters in the string are numbered from 1. If there are several possible solutions, output any of them.
If the solution doesn't exist, output «NO» (without quotes).
Examples
input
output
input
output
input
output
题意:给你一个字符串,删除一个字符,是否形成回文,如果有,输出删除的位置;
思路:对于删除一个字符以后,这个字符串的对称轴的位置只有两个,所有枚举对称轴,复杂度O(n);
K. Palindromization
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output
Mihahim has a string s. He wants to delete exactly one character from it so that the resulting string would be a palindrome. Determine if he can do it, and if he can, what character should be deleted.
Input
The input contains a string s of length (2 ≤ |s| ≤ 200000), consisting of lowercase Latin letters.
Output
If the solution exists, output «YES» (without quotes) in the first line. Then in the second line output a single integer x — the number of the character that should be removed from s so that the resulting string would be a palindrome. The characters in the string are numbered from 1. If there are several possible solutions, output any of them.
If the solution doesn't exist, output «NO» (without quotes).
Examples
input
evertree
output
YES 2
input
emerald
output
NO
input
aa
output
YES 2
题意:给你一个字符串,删除一个字符,是否形成回文,如果有,输出删除的位置;
思路:对于删除一个字符以后,这个字符串的对称轴的位置只有两个,所有枚举对称轴,复杂度O(n);
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #include<vector> using namespace std; #define LL long long const int N=2e5+100,M=1e6+10,inf=1e9+10; const LL INF=1e18+10,mod=19260817; char a ; int checkl(int l,int r,int n,int L) { while(r<=n) { while(a[l]!=a[r]) { if(L!=1)return 0; L=l; l--; } l--;r++; } return L; } int checkr(int l,int r,int n,int R) { while(l>=1) { while(a[l]!=a[r]) { if(R!=n)return 0; else R=r; r++; } l--;r++; } return R; } int main() { scanf("%s",a+1); int n=strlen(a+1); if(n%2==0) { int ans=checkl(n/2,n/2+2,n,1); if(ans)return 0*printf("YES\n%d\n",ans); ans=checkr(n/2-1,n/2+1,n,n); if(ans)return 0*printf("YES\n%d\n",ans); puts("NO"); } else { int ans=checkl(n/2+1,n/2+2,n,1); if(ans)return 0*printf("YES\n%d\n",ans); ans=checkr(n/2,n/2+1,n,n); if(ans)return 0*printf("YES\n%d\n",ans); puts("NO\n"); } return 0; }
相关文章推荐
- Codeforces gym 100971 D. Laying Cables 单调栈
- codeforces Gym 100971 A、B、C、F、G、K、L
- Codeforces Gym 101505D Orchard Division(离散化+树状数组+扫描线+二分)
- Codeforces Gym 101064E A Word to Trump All [AC自动机+bfs]
- Codeforces Gym 100114 H. Milestones 离线树状数组
- CodeForces Gym 101745 简要题解
- Codeforces Gym 100379L Game with a string 博弈+交互题
- [构造][度数序列的可图性]Codeforces Gym 100269K. Kids in a Friendly Class
- 【codeforces gym 100187J】 【dfs判连通】Deck Shuffling 【给你一堆牌和一些洗牌机,后者可以改变牌的顺序,问你能不能把数字为x的牌洗到第一个位置。】
- Codeforces Gym 101608 G WiFi Password(尺取/二分+ST表 )
- codeforces #322 div 2 B. Luxurious Houses (思路)
- Codeforces gym 101149 K 想法
- CodeForcesGym 100212E Long Dominoes
- Codeforces Gym 100340A Cookies
- codeforces gym 101142 F(思维)
- Codeforces Gym 100642 C D F
- CodeForces Gym 100989C 1D Cafeteria (A)
- CodeForcesGym 100733D Little thief Shi
- CodeforcesGym - 101102D. Rectangles 单调栈+dp优化
- Codeforces Round #293 (Div. 2) A. Vitaly and Strings(思路)