日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Palindrome Index

發布時間:2025/3/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Palindrome Index 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

傳送門:?Palindrome Index

Problem Statement

You are given a string of lower case letters. Your task is to figure out the index of the character on whose removal it will make the string a palindrome. There will always be a valid solution.?

In case the string is already a palindrome, then?-1?is also a valid answer along with possible indices.

Input Format

The first line contains?T, i.e. the number of test cases.
T?lines follow, each containing a string.

Output Format

Print the position (0 index) of the letter by removing which the string turns into a palindrome. For a string, such as

bcbc

we can remove?b?at index 0 or?c?at index 3. Both answers are accepted.

Constraints?
1T20?
1?length of string?100005?
All characters are Latin lower case indexed.

Sample Input

3 aaab baa aaa

Sample Output

3 0 -1

Explanation

In the given input,?T?= 3,

  • For input?aaab, we can see that removing?b?from the string makes the string a palindrome, hence the position 3.
  • For input?baa, removing?b?from the string makes the string palindrome, hence the position 0.
  • As the string?aaa?is already a palindrome, you can output 0, 1 or 2 as removal of any of the characters still maintains the palindrome property. Or you can print -1 as this is already a palindrome.

讀題時需注意:

題目中先說 “There will always be a valid solution. ”,然后才說“In case the string is already a palindrome, then?-1?is also a valid answer along with possible indices.”。注意體會這句話,我們首先應注意到,即使輸入的字符串S是個回文串,也可以刪除某個字母使其仍為回文串。如果|S|為奇數,則刪除中間那個字母,結果串仍為回文串。如果|S|為偶數則刪除中間兩個相等字符中的任一個,結果串也回文。

完全暴力的解法:

枚舉要刪除的字母,檢查結果串是否回文。復雜度O(N^2)。

1 #include<bits/stdc++.h> 2 using namespace std; 3 const int MAX_N=1e5+10; 4 char s[MAX_N]; 5 int len; 6 int opp(int j, int x){ 7 if(x==0){ 8 return len+1-j; 9 } 10 if(j<x){ 11 return len-j<x? len-j: len-j+1; 12 } 13 else{ 14 return len+2-j; 15 } 16 } 17 bool ok(int x){ 18 int tmp=x?(len-1)>>1:len>>1; 19 for(int i=0, j=1; i<tmp; i++, j++){ 20 if(j==x){ 21 j++; 22 } 23 if(s[j]!=s[opp(j, x)]){ 24 return false; 25 } 26 } 27 return true; 28 } 29 int main(){ 30 int T; 31 scanf("%d", &T); 32 while(T--){ 33 scanf("%s", s+1); 34 len=strlen(s+1); 35 for(int i=0; i<=len; i++){ 36 if(ok(i)){ 37 printf("%d\n", i-1); 38 break; 39 } 40 } 41 } 42 return 0; 43 }

只是這解法過于暴力,TLE。

下面就要引入這道題給我的最大啟示了:

尋找有助于簡化問題的必要條件

考慮一下上面的單純暴力算法有那些冗余計算。

首先必須指出一個問題:優化算法的途徑是充分考慮問題的特殊性。

其次要注意到:題目要求的是存在性判別,上面的算法枚舉被刪除字符的位置是無可厚非的。

接著考慮一下使上面的算法達到最壞情況的數據:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab

在這種情況下,上述算法必須枚舉到最后一個字符才能確定答案。

我們不難發現一個問題

?

?

?

轉載于:https://www.cnblogs.com/Patt/p/4467609.html

新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!

總結

以上是生活随笔為你收集整理的Palindrome Index的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 北条麻妃一区二区三区在线观看 | 美女隐私免费观看 | 台湾男男gay做爽爽的视频 | 激情 小说 亚洲 图片 伦 | 精品久久一区 | 一区二区欧美日韩 | 日韩乱码人妻无码系列中文字幕 | 日本少妇三级 | 日韩一级理论片 | 在线播放成人av | 一级成人毛片 | 老司机精品福利导航 | 香蕉911| 精品国产一区二区三区久久久 | 1级黄色大片儿 | 中文字幕观看视频 | 激情小说视频在线 | 乱人伦xxxx国语对白 | 伊人影音 | 视频在线观看你懂的 | 亚洲啪 | 日本极品少妇 | 在线色综合 | 日本美女裸体视频 | 中日韩男男gay无套 人人草人人干 | 波多野结衣中文字幕久久 | 国产精品福利小视频 | 亚洲国产成人一区二区精品区 | 视频精品久久 | 亚洲玖玖玖 | 国产成人精品一区二区在线小狼 | 不卡一区二区在线 | 亚洲天堂777 | 欧美自拍区 | 性感美女一区二区三区 | 欧美丰满熟妇bbbbbb百度 | 在线中文一区 | 国产精品视频亚洲 | av漫画在线观看 | 一区二区三区不卡视频 | 亚洲成人免费在线视频 | 日本三级午夜理伦三级三 | 欧美yyy| a国产 | 久久澡 | www国产黄色 | 草草影院第一页yycc.com | 福利在线免费观看 | 免费久久久 | www.爱爱| 黑人一区二区三区四区五区 | 日日操夜夜操视频 | 国产在线播放一区二区三区 | 国产在线看片 | 久久最新免费视频 | 青青草视频在线看 | 日日摸夜夜 | 美女露胸无遮挡 | jlzzjlzz亚洲女人18 | 丁香婷婷综合激情 | 成人片黄网站久久久免费 | 日本69少妇| 午夜久久久久久噜噜噜噜 | 免费看黄禁片 | 欧美一级影院 | 美女扒开腿让男生捅 | 国产高中女学生第一次 | 午夜伦伦 | 激情春色网 | 桃色视频网站 | 视频三区在线 | 久久婷婷综合色丁香五月 | 草逼国产 | 亚洲欧美国产视频 | 久久中文一区 | 欧美精品自拍视频 | 欧美一级免费看 | 日韩天堂av | 欧美aⅴ在线 | 三级在线免费 | 国产中文网 | 国内成人自拍 | 久久精品国产亚洲a | 奶水喷溅 在线播放 | 污污网站免费 | 欧美日韩一二三四 | 亚洲精品久久久狠狠狠爱 | 二级毛片在线观看 | 成人在线免费 | 国产欧美亚洲一区二区 | 性开放的欧美大片 | 少妇熟女一区二区三区 | 国产第一亚洲 | 精品国自产拍在线观看 | 亚洲天堂不卡 | 欧美人妻精品一区二区 | 国产美女无遮挡免费视频 | 日韩欧美一区二区视频 | av有码在线观看 |