POJ 1302
題意
給出一串字符,求變異后的字符串
思路
- 如果是字母,這當前字母的位置變為后面需要變異字母的總和
- 如果是數字1-9,則當前數字改變為當前數字減1,并跳過當前數字的個數繼續向后執行。
英語不太好,網上查的題意。
遞歸也不太會,網上學習的poj1302解題報告
#include <iostream>
#include <cstring>
using namespace std;
char ss[15];
int n;
char str[25];
const char a1[] = "ENDOFINPUT";
int solve(int p){
if(str[p] == '0'|| p == n)
return 0;
if(str[p]>='A'&&str[p]<='Z'){
int a = solve(p+1);
str[p] = a%10 + '0';
return a+1;
}
if(str[p]>='0'&&str[p]<='9'){
str[p] -= 1;
if(p+(str[p]-'0')+1<n){
int b = solve(p+(str[p]-'0')+1);
return b+1;
}else{
int c = solve(p+1);
return c+1;
}
}
}
int main(){
while(1){
cin>>ss;
if(strcmp(ss,a1) == 0)
break;
cin>>n>>str;
cin>>ss;
solve(0);
cout<<str<<endl;
}
return 0;
}