AtCoder Beginner Contest 066 B - ss
题目链接:http://abc066.contest.atcoder.jp/tasks/abc066_b
Time limit : 2sec / Memory limit : 256MB
Score : 200 points
Problem Statement
We will call a string that can be obtained by concatenating two equal strings an even string. For example, xyzxyz
and aaaaaa
are even, while ababab
and xyzxy
are not.
You are given an even string S consisting of lowercase English letters. Find the length of the longest even string that can be obtained by deleting one or more characters from the end of S. It is guaranteed that such a non-empty string exists for a given input.
Constraints
- 2≤|S|≤200
- S is an even string consisting of lowercase English letters.
- There exists a non-empty even string that can be obtained by deleting one or more characters from the end of S.
Input
Input is given from Standard Input in the following format:
S
Output
Print the length of the longest even string that can be obtained.
Sample Input 1
abaababaab
Sample Output 1
6
abaababaab
itself is even, but we need to delete at least one character.abaababaa
is not even.abaababa
is not even.abaabab
is not even.abaaba
is even. Thus, we should print its length, 6.
Sample Input 2
xxxx
Sample Output 2
2
xxx
is not even.xx
is even.
Sample Input 3
abcabcabcabc
Sample Output 3
6
The longest even string that can be obtained is abcabc
, whose length is 6.
Sample Input 4
akasakaakasakasakaakas
Sample Output 4
14
The longest even string that can be obtained is akasakaakasaka
, whose length is 14.
题解:还是比较有趣 如果前一半字符和后一半字符一样就输出总字符数 否则字符串尾删除一个字符
那就用栈吧 不满足的话就用栈删除很方便
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <vector> 6 #include <cstdlib> 7 #include <iomanip> 8 #include <cmath> 9 #include <ctime> 10 #include <map> 11 #include <set> 12 #include <queue> 13 #include <stack> 14 using namespace std; 15 #define lowbit(x) (x&(-x)) 16 #define max(x,y) (x>y?x:y) 17 #define min(x,y) (x<y?x:y) 18 #define MAX 100000000000000000 19 #define MOD 1000000007 20 #define pi acos(-1.0) 21 #define ei exp(1) 22 #define PI 3.141592653589793238462 23 #define INF 0x3f3f3f3f3f 24 #define mem(a) (memset(a,0,sizeof(a))) 25 typedef long long ll; 26 ll gcd(ll a,ll b){ 27 return b?gcd(b,a%b):a; 28 } 29 const int N=100005; 30 const int mod=1e9+7; 31 32 int main() 33 { 34 std::ios::sync_with_stdio(false); 35 char a[201]; 36 scanf("%s",a); 37 stack<char> s; 38 int len=strlen(a); 39 for(int i=0;i<len;i++) 40 s.push(a[i]); 41 s.pop(); 42 int t,flag; 43 while(1){ 44 if(s.size()%2==1) s.pop(); 45 else { 46 t=s.size(); 47 t/=2; 48 flag=1; 49 for(int i=0;i<t;i++){ 50 if(a[i]!=a[i+t]){ 51 flag=0; 52 break; 53 } 54 } 55 if(flag){ 56 cout<<t*2<<endl; 57 break; 58 } 59 else s.pop(); 60 } 61 } 62 return 0; 63 }