POJ3096:Surprising Strings(map)
http://poj.org/problem?id=3096
for循环真是奇妙!
#include <string.h> #include <stdio.h> #include <string> #include <map> using namespace std; int main() { char a[100],st[3]; int l,flag; while(scanf("%s",a)!=EOF) { if(a[0]=='*') break; l=strlen(a); if(l<=2) { printf("%s is surprising.\n",a); continue; } flag=0; for(int i=0;i<=l-2;i++) { map<string,int>q;//相当于定义了局部变量,每次执行for循环相当与重新赋值或者q.clear(); for(int j=0;j<=l-2-i;j++) { st[0]=a[j]; st[1]=a[j+1+i];//i代表间隔 st[2]='\0'; q[st]++; if(q[st]>=2) { flag=1; break; } } if(flag==1) { printf("%s is NOT surprising.\n",a); break; } } if(flag==0) printf("%s is surprising.\n",a); } return 0; }