2015年第六届蓝桥杯C/C++ A组国赛 —— 第五题:切开字符串
標題:切開字符串
Pear有一個字符串,不過他希望把它切成兩段。
這是一個長度為N(<=10^5)的字符串。
Pear希望選擇一個位置,把字符串不重復不遺漏地切成兩段,長度分別是t和N-t(這兩段都必須非空)。
Pear用如下方式評估切割的方案:
定義“正回文子串”為:長度為奇數的回文子串。
設切成的兩段字符串中,前一段中有A個不相同的正回文子串,后一段中有B個不相同的非正回文子串,則該方案的得分為A*B。
注意,后一段中的B表示的是:“…非正回文…”,而不是: “…正回文…”。
那么所有的切割方案中,A*B的最大值是多少呢?
【輸入數據】
輸入第一行一個正整數N(<=10^5)
接下來一行一個字符串,長度為N。該字符串僅包含小寫英文字母。
【輸出數據】
一行一個正整數,表示所求的A*B的最大值。
【樣例輸入】
10
bbaaabcaba
【樣例輸出】
38
【數據范圍】
對于20%的數據,N<=100
對于40%的數據,N<=1000
對于100%的數據,N<=10^5
資源約定:
峰值內存消耗 < 512M
CPU消耗 < 1000ms
請嚴格按要求輸出,不要畫蛇添足地打印類似:“請您輸入…” 的多余內容。
所有代碼放在同一個源文件中,調試通過后,拷貝提交該源碼。
注意: main函數需要返回0
注意: 只使用ANSI C/ANSI C++ 標準,不要調用依賴于編譯環境或操作系統的特殊函數。
注意: 所有依賴的函數必須明確地在源文件中 #include , 不能通過工程設置而省略常用頭文件。
提交時,注意選擇所期望的編譯器類型。
Code
/*^....0^ .1 ^1^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^0. ^ 0^.0 1 .^.1 ^0 .........001^.1 1. .111100....01^00 11^ ^1. .1^1.^ ^0 0^.^ ^0..1.1 1..^1 .0 ^ ^00. ^^0.^^ 0 ^^110.^0 0 ^ ^^^10.01^^ 10 1 1 ^^^1110.101 10 1.1 ^^^1111110010 01 ^^ ^^^1111^1.^ ^^^10 10^ 0^ 1 ^^111^^^0.1^ 1....^11 0 ^^11^^^ 0.. ....1^ ^ ^1. 0^ ^11^^^ ^ 1 111^ ^ 0.10 00 11 ^^^^^ 1 0 1.0^ ^0 ^0 ^^^^ 0 0.0^ 1.0 .^ ^^^^ 1 1 .0^.^ ^^ 0^ ^1 ^^^^ 0. ^.11 ^ 11 1. ^^^ ^ ^ ..^^..^ ^1 ^.^ ^^^ .0 ^.00..^ ^0 01 ^^^ .. 0..^1 .. .1 ^.^ ^^^ 1 ^ ^0001^ 1. 00 0. ^^^ ^.0 ^.1. 0^. ^.^ ^.^ ^^^ ..0.01 .^^. .^ 1001 ^^ ^^^ . 1^. ^ ^. 11 0. 1 ^ ^^ 0.0 ^. 0 ^0 1 ^^^ 0.0.^ 1. 0^ 0 .1 ^^^ ...1 1. 00 . .1 ^^^ ..1 1. ^. 0 .^ ^^ ..0. 1. .^ . 0 ..1 1. 01 . . ^ 0^.^ 00 ^0 1. ^ 1 1.0 00 . ^^^^^^ ..^ 00 01 ..1. 00 10 1 ^^.1 00 ^. ^^^ .1.. 00 .1 1..01 ..1.1 00 1. ..^ 10^ 1^ 00 ^.1 0 1 1.1 00 00 ^ 1 ^. 00 ^.^ 10^ ^^1.1 00 00 10^..^ 1. ^. 1.0 1 ^. 00 00 .^^ ^. ^ 1 00 ^0000^ ^ 011 0 ^. 00.0^ ^00000 1.00.1 11. 1 0 1^^0.01 ^^^ 01.^ ^ 1 1^^ ^.^1 1 0... 1 ^1 1^ ^ .01 ^ 1.. 1.1 ^0.0^ 0 1..01^^100000..0^1 1 ^ 1 ^^1111^ ^^0 ^ ^ 1 1000^.1 ^.^ . 00.. 1.1 0. 01. . 1. .^1. 1 1. ^0^ . ^.1 00 01^.0 001. .^*//* Procedural objectives:Variables required by the program:Procedural thinking:Functions required by the program:Determination algorithm:Determining data structure:*/ /* My dear Max said: "I like you, So the first bunch of sunshine I saw in the morning is you, The first gentle breeze that passed through my ear is you, The first star I see is also you. The world I see is all your shadow."FIGHTING FOR OUR FUTURE!!! */ #include <set> #include <string> #include <iostream>using namespace std;bool is_palindrome_string(string str){int len=str.length();if(len%2==0) return false;for(int i=0;i<len/2;i++)if(str[i]!=str[len-i-1])return false;return true; }int main(){string str;set<string> temp0,temp1;int N,Left[100007],Right[100007];cin>>N>>str;for(int i=0;i<N;i++){for(int j=i;j>-1;j-=2){int t=i-j+1;string s1(str,j,t);if(is_palindrome_string(s1))temp0.insert(s1);}Left[i]=temp0.size();}for(int i=N-1;i>-1;i--){for(int j=i;j<N;j++){int t=j-i+1;string s1(str,i,t);if(!is_palindrome_string(s1))temp1.insert(s1);}Right[i]=temp1.size();}long long ans=-1;for(int i=1;i<N;i++)ans=max(ans,(long long)Left[i-1]*Right[i]);cout<<ans<<endl;return 0; }總結
以上是生活随笔為你收集整理的2015年第六届蓝桥杯C/C++ A组国赛 —— 第五题:切开字符串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSDN修改博客皮肤模板
- 下一篇: 2019第十届蓝桥杯C/C++ A组省赛