Codeforces Round #410 (Div. 2)-A

题目简述:给定一个字符串判断是否能够恰好改变一个字符使其变成回文(回文:eg.abcba或abccba)。

注意:1,是恰好改变一个字符,恰好!

           2,字符串例如“abc”这样的奇数个数的而且是完整回文的字符串也是可以恰好改变一个字符变成回文的(改变最中间的那个字符,中间的那个字符改成什么都是回文)

我当时就卡死在这个坑。

我的代码是用栈做的虽然有点蠢...当时我都不知道for居然可以放两个入口变量,那样的话i++,j--就成了,我着急直接用栈,我居然不知道栈没有迭代器,也没注意上述注意事项2,结果就gg了。

那种简洁的做法我就不写了。

代码如下:

@font-face { font-family: "Times New Roman"; }@font-face { font-family: "宋体"; }@font-face { font-family: "Calibri"; }p.MsoNormal { margin: 0pt 0pt 0.0001pt; text-align: justify; font-family: Calibri; font-size: 10.5pt; }span.msoIns { text-decoration: underline; color: blue; }span.msoDel { text-decoration: line-through; color: red; }div.Section0 { }

#include<bits/stdc++.h>

using namespace std;

int main(){

stack<char> st;

char c[100];

int d_num;

while(gets(c)){

d_num=0;

if(strlen(c)<1){

cout<<"NO"<<endl;

continue;

}

for(int i=0;i<strlen(c);i++){

st.push(c[i]);

}

for(int i=0;i<strlen(c);i++){

if(c[i]!=st.top())  d_num++;

st.pop();

}

if((strlen(c)%2)&&d_num==0)  d_num+=2;

if(d_num==2)  cout<<"YES"<<endl;

else  cout<<"NO"<<endl;

}

return 0;

}

原文地址:https://www.cnblogs.com/sunowsir/p/6750213.html