CFgym:Palindromization(字符串Hash)
2017-05-12 01:36
393 查看
K. Palindromization
time limit per test
2 seconds
memory limit per test
256 megabytes
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
题意:给出一个字符串,判断能否删除一个字符使它变成回文串。
思路:正反预处理字符串的哈希值用数组存下前缀和,枚举要消掉的字符即可。
# include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N = 2e5+3;
char s
;
ull a
, b
,mod = 1e9+7;
int main()
{
int len, f=0;
ull pow=1, seed=23;
scanf("%s",s+1);
len = strlen(s+1);
for(int i=1; i<=len; ++i)
{
a[i] = (a[i-1] + s[i]*pow)%mod;
pow = (pow*seed)%mod;
}
pow = 1;
for(int i=len; i>=1; --i)
{
b[i] = (b[i+1] + s[i]*pow)%mod;
pow = (pow*seed)%mod;
}
for(int i=1; i<=len; ++i)
{
ull l = ((a[i-1]*seed)%mod + (a[len]-a[i]+mod)%mod)%mod;
ull r = ((b[i+1]*seed)%mod + (b[1]-b[i]+mod)%mod)%mod;
if(l==r)
{
f = 1;
printf("YES\n%d\n",i);
break;
}
}
if(!f) puts("NO");
return 0;
}
4000
time limit per test
2 seconds
memory limit per test
256 megabytes
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
题意:给出一个字符串,判断能否删除一个字符使它变成回文串。
思路:正反预处理字符串的哈希值用数组存下前缀和,枚举要消掉的字符即可。
# include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N = 2e5+3;
char s
;
ull a
, b
,mod = 1e9+7;
int main()
{
int len, f=0;
ull pow=1, seed=23;
scanf("%s",s+1);
len = strlen(s+1);
for(int i=1; i<=len; ++i)
{
a[i] = (a[i-1] + s[i]*pow)%mod;
pow = (pow*seed)%mod;
}
pow = 1;
for(int i=len; i>=1; --i)
{
b[i] = (b[i+1] + s[i]*pow)%mod;
pow = (pow*seed)%mod;
}
for(int i=1; i<=len; ++i)
{
ull l = ((a[i-1]*seed)%mod + (a[len]-a[i]+mod)%mod)%mod;
ull r = ((b[i+1]*seed)%mod + (b[1]-b[i]+mod)%mod)%mod;
if(l==r)
{
f = 1;
printf("YES\n%d\n",i);
break;
}
}
if(!f) puts("NO");
return 0;
}
4000
相关文章推荐
- poj3007 Organize Your Train part II, 字符串hash
- Contest 20140914 Mushroom写情书 字符串雙hash 後綴數組
- 编程珠玑 第十五章 散列表应用 hash字符串
- 字符串Hash的原理与应用
- ACdreamoj 1011(树状数组维护字符串hash前缀和)
- hdu 1880 魔咒词典(字符串hash)
- 洛谷P3832 [NOI2017]蚯蚓排队 【链表 + 字符串hash】
- hdu 6208 hash字符串水题
- [字符串hash][堆排序][AC自动机][usaco3.1.5]Contact
- zjnuSAVEZ (字符串hash)
- hdu4821 字符串hash
- HDU 1880 字符串hash 入门题
- 字符串哈希(Hash模板)
- 字符串Hash:ELFhash 函数及其变体
- hdu4821 String 字符串hash(bkdrhash)
- POJ1200 Crazy Search (字符串hash)
- P2264 情书(字符串hash90分)
- UVA 11019 Matrix Matcher(字符串hash+KMP)
- [字符串hash] hdu 4821 String
- 字符串hash+线段树 Codeforces580E Kefa and Watch