AOJ ITP1 9.String
はい。
http://judge.u-aizu.ac.jp/onlinejudge/topic.jsp?cid=ITP1#problems/ITP1_9
A.Finding a Word
ans=[] W=raw_input() while True: i = raw_input() if i == 'END_OF_TEXT': break ans+=[j.lower() for j in i.split()] print ans.count(W)
C++11
#include<bits/stdc++.h> #include<vector> #include<algorithm> using namespace std; int main(){ double pai=3.141592653589; char chk[10], t[1000]; int ans=0; string ask,tmp; scanf("%s",&chk); ask=chk; transform(ask.begin(),ask.end(),ask.begin(),::toupper); for(;;){ scanf("%s",&t); tmp=t; if(tmp=="END_OF_TEXT") break; transform(tmp.begin(),tmp.end(),tmp.begin(),::toupper); if(tmp==ask){ ans++; } } printf("%d\n",ans); return 0; }
大文字小文字を何も変更しない時のEND_OF_TEXTと厳密に一致した文字列で終了の判定をする必要があるので注意。
B.Shuffle
while 1: w=raw_input() if w=='-':break n=input() for i in range(n): x=input() w=w[x:]+w[:x] print w
C++11
#include<bits/stdc++.h> #include<vector> #include<algorithm> using namespace std; int main(){ double pai=3.141592653589; int m,h,l; char s[200]; string ans,tmp1,tmp2; for(;;){ scanf("%s",&s); tmp1=s; if(tmp1=="-"){ break; } scanf("%d",&m); l=tmp1.length(); for(int i=0;i<m;i++){ scanf("%d",&h); ans=""; ans.assign(tmp1,h,l-h+1); tmp2.assign(tmp1,0,h); ans+=tmp2; tmp1=ans; } printf("%s\n",ans.c_str()); } return 0; }
ソース内で文字を指定するのが0始まりだったりするのでシャッフルの指定する番目にだけ注意すれば大丈夫だと思う。
C.Card Game
n=input() t=h=0 for i in range(n): x,y=map(str,raw_input().split()) if x==y: t+=1 h+=1 elif x<y: h+=3 else: t+=3 print t,h
C++11
#include<bits/stdc++.h> #include<vector> #include<algorithm> using namespace std; int main(){ double pai=3.141592653589; int n,taro=0,hanako=0; char t[100],h[100]; string tarostr,hanakostr; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%s %s",&t,&h); tarostr=t; hanakostr=h; if(tarostr==hanakostr){ taro++; hanako++; }else if(tarostr<hanakostr){ hanako+=3; }else{ taro+=3; } } printf("%d %d\n",taro,hanako); return 0; }
辞書順比較が>=<の記号だけで比較できるならそれで解決します。
D.Transformation
w=raw_input() n=input() l=['print','replace','reverse'] for i in range(n): c=map(str,raw_input().split()) if c[0]==l[0]: print w[int(c[1]):int(c[2])+1] elif c[0]==l[1]: w=w[:int(c[1])]+c[3]+w[int(c[2])+1:] else: w=w[0:int(c[1])]+w[int(c[1]):int(c[2])+1][::-1]+w[int(c[2])+1:]
C++11
#include<bits/stdc++.h> #include<vector> #include<algorithm> using namespace std; int main(){ double pai=3.141592653589; int n,a,b; char c[1000],rc[1000],s[10]; scanf("%s",&c); scanf("%d",&n); string w; for(int i=0;i<n;i++){ scanf("%s",&s); w=s; if(w=="print"){ scanf("%d %d",&a,&b); for(int i=a;i<=b;i++){ printf("%c",c[i]); } printf("\n"); }else if(w=="replace"){ scanf("%d %d %s",&a,&b,&rc); int j=0; for(int i=a;i<=b;i++){ c[i]=rc[j]; j++; } }else{ scanf("%d %d",&a,&b); int j=b; strcpy(rc,c); for(int i=a;i<=b;i++){ c[i]=rc[j]; j--; } } } return 0; }
pythonは配列で1文字ずつリストすると並び替えたり、1文字だけ他の文字に変えたりも自由に出来ます。str型ですと1文字だけをw[2]=’S'
というような変更の仕方は出来ないです。C++だとcharでもそれに近い操作方法で出来るのでそれで。string型にするとこれも色々不自由っぽいです。